-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shawn Hurley
committed
Oct 18, 2018
1 parent
956ccda
commit c470efe
Showing
7 changed files
with
667 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
// Copyright 2018 The Operator-SDK Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package status | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/operator-framework/operator-sdk/pkg/ansible/runner/eventapi" | ||
"github.com/sirupsen/logrus" | ||
"k8s.io/api/core/v1" | ||
meta "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
const ( | ||
host = "localhost" | ||
) | ||
|
||
// AnsibleStatus - encapsulation of the ansible result. | ||
type AnsibleStatus struct { | ||
Ok int `json:"ok"` | ||
Changed int `json:"changed"` | ||
Skipped int `json:"skipped"` | ||
Failures int `json:"failures"` | ||
TimeOfCompletion eventapi.EventTime `json:"completion"` | ||
} | ||
|
||
// NewAnsibleStatusFromStatusJobEvent - creates a Ansible status from job event. | ||
func NewAnsibleStatusFromStatusJobEvent(je eventapi.StatusJobEvent) *AnsibleStatus { | ||
// ok events. | ||
o := 0 | ||
changed := 0 | ||
skipped := 0 | ||
failures := 0 | ||
if v, ok := je.EventData.Changed[host]; ok { | ||
changed = v | ||
} | ||
if v, ok := je.EventData.Ok[host]; ok { | ||
o = v | ||
} | ||
if v, ok := je.EventData.Skipped[host]; ok { | ||
skipped = v | ||
} | ||
if v, ok := je.EventData.Failures[host]; ok { | ||
failures = v | ||
} | ||
return &AnsibleStatus{ | ||
Ok: o, | ||
Changed: changed, | ||
Skipped: skipped, | ||
Failures: failures, | ||
TimeOfCompletion: je.Created, | ||
} | ||
} | ||
|
||
// NewAnsibleStatusFromMap - creates a Ansible status from a job event. | ||
func NewAnsibleStatusFromMap(sm map[string]interface{}) *AnsibleStatus { | ||
//Create Old top level status | ||
// ok events. | ||
o := 0 | ||
changed := 0 | ||
skipped := 0 | ||
failures := 0 | ||
e := eventapi.EventTime{} | ||
if v, ok := sm["changed"]; ok { | ||
changed = int(v.(int64)) | ||
} | ||
if v, ok := sm["ok"]; ok { | ||
o = int(v.(int64)) | ||
} | ||
if v, ok := sm["skipped"]; ok { | ||
skipped = int(v.(int64)) | ||
} | ||
if v, ok := sm["failures"]; ok { | ||
failures = int(v.(int64)) | ||
} | ||
if v, ok := sm["completion"]; ok { | ||
s := v.(string) | ||
e.UnmarshalJSON([]byte(s)) | ||
} | ||
return &AnsibleStatus{ | ||
Ok: o, | ||
Changed: changed, | ||
Skipped: skipped, | ||
Failures: failures, | ||
TimeOfCompletion: e, | ||
} | ||
} | ||
|
||
// ConditionType - type of condition | ||
type ConditionType string | ||
|
||
const ( | ||
// RunningConditionType - condition type of running. | ||
RunningConditionType ConditionType = "Running" | ||
// FailureConditionType - condition type of failure. | ||
FailureConditionType ConditionType = "Failure" | ||
) | ||
|
||
// Condition - the condition for the ansible operator. | ||
type Condition struct { | ||
Type ConditionType `json:"type"` | ||
Status v1.ConditionStatus `json:"status"` | ||
LastTransitionTime meta.Time `json:"lastTransitionTime"` | ||
AnsibleStatus *AnsibleStatus `json:"ansibleStatus"` | ||
Reason string `json:"reason"` | ||
Message string `json:"message"` | ||
} | ||
|
||
func createConditionFromMap(cm map[string]interface{}) Condition { | ||
ct, ok := cm["type"].(string) | ||
if !ok { | ||
//If we do not find the string we are defaulting | ||
// to make sure we can at least update the status. | ||
ct = string(RunningConditionType) | ||
} | ||
status, ok := cm["status"].(string) | ||
if !ok { | ||
status = string(v1.ConditionTrue) | ||
} | ||
reason, ok := cm["reason"].(string) | ||
if !ok { | ||
reason = RunningReason | ||
} | ||
message, ok := cm["message"].(string) | ||
if !ok { | ||
message = RunningMessage | ||
} | ||
asm, ok := cm["ansibleStatus"].(map[string]interface{}) | ||
var ansibleStatus *AnsibleStatus | ||
if ok { | ||
ansibleStatus = NewAnsibleStatusFromMap(asm) | ||
} | ||
ltts, ok := cm["lastTransitionTime"].(string) | ||
ltt := meta.Now() | ||
if ok { | ||
t, err := time.Parse("2006-01-02T15:04:05Z", ltts) | ||
if err != nil { | ||
logrus.Warningf("unable to parse time for status condition: %v", ltts) | ||
} else { | ||
ltt = meta.NewTime(t) | ||
} | ||
} | ||
return Condition{ | ||
Type: ConditionType(ct), | ||
Status: v1.ConditionStatus(status), | ||
LastTransitionTime: ltt, | ||
Reason: reason, | ||
Message: message, | ||
AnsibleStatus: ansibleStatus, | ||
} | ||
} | ||
|
||
// Status - The status for custom resoureces managed by the operator-sdk. | ||
type Status struct { | ||
Conditions []Condition `json:"conditions"` | ||
} | ||
|
||
// CreateStatusFromMap - create a status from the map | ||
func CreateStatusFromMap(statusMap map[string]interface{}) Status { | ||
conditionsInterface, ok := statusMap["conditions"].([]interface{}) | ||
if !ok { | ||
return Status{Conditions: []Condition{}} | ||
} | ||
conditions := []Condition{} | ||
for _, ci := range conditionsInterface { | ||
cm, ok := ci.(map[string]interface{}) | ||
if !ok { | ||
logrus.Warningf("unknow condition, remoiong condition: %v", ci) | ||
continue | ||
} | ||
conditions = append(conditions, createConditionFromMap(cm)) | ||
} | ||
return Status{Conditions: conditions} | ||
} |
Oops, something went wrong.