forked from operator-framework/operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
146 lines (127 loc) · 4.61 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// 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 types
import (
"encoding/json"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
type HelmAppList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`
Items []HelmApp `json:"items"`
}
type HelmApp struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata"`
Spec HelmAppSpec `json:"spec"`
Status HelmAppStatus `json:"status,omitempty"`
}
type HelmAppSpec map[string]interface{}
type HelmAppConditionType string
type ConditionStatus string
type HelmAppConditionReason string
type HelmAppCondition struct {
Type HelmAppConditionType `json:"type"`
Status ConditionStatus `json:"status"`
Reason HelmAppConditionReason `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`
}
type HelmAppRelease struct {
Name string `json:"name,omitempty"`
Manifest string `json:"manifest,omitempty"`
}
const (
ConditionInitialized HelmAppConditionType = "Initialized"
ConditionDeployed HelmAppConditionType = "Deployed"
ConditionReleaseFailed HelmAppConditionType = "ReleaseFailed"
ConditionIrreconcilable HelmAppConditionType = "Irreconcilable"
StatusTrue ConditionStatus = "True"
StatusFalse ConditionStatus = "False"
StatusUnknown ConditionStatus = "Unknown"
ReasonInstallSuccessful HelmAppConditionReason = "InstallSuccessful"
ReasonUpdateSuccessful HelmAppConditionReason = "UpdateSuccessful"
ReasonUninstallSuccessful HelmAppConditionReason = "UninstallSuccessful"
ReasonInstallError HelmAppConditionReason = "InstallError"
ReasonUpdateError HelmAppConditionReason = "UpdateError"
ReasonReconcileError HelmAppConditionReason = "ReconcileError"
ReasonUninstallError HelmAppConditionReason = "UninstallError"
)
type HelmAppStatus struct {
Conditions []HelmAppCondition `json:"conditions"`
DeployedRelease *HelmAppRelease `json:"deployedRelease,omitempty"`
}
func (s *HelmAppStatus) ToMap() (map[string]interface{}, error) {
var out map[string]interface{}
jsonObj, err := json.Marshal(&s)
if err != nil {
return nil, err
}
if err := json.Unmarshal(jsonObj, &out); err != nil {
return nil, err
}
return out, nil
}
// SetCondition sets a condition on the status object. If the condition already
// exists, it will be replaced. SetCondition does not update the resource in
// the cluster.
func (s *HelmAppStatus) SetCondition(condition HelmAppCondition) *HelmAppStatus {
now := metav1.Now()
for i := range s.Conditions {
if s.Conditions[i].Type == condition.Type {
if s.Conditions[i].Status != condition.Status {
condition.LastTransitionTime = now
} else {
condition.LastTransitionTime = s.Conditions[i].LastTransitionTime
}
s.Conditions[i] = condition
return s
}
}
// If the condition does not exist,
// initialize the lastTransitionTime
condition.LastTransitionTime = now
s.Conditions = append(s.Conditions, condition)
return s
}
// RemoveCondition removes the condition with the passed condition type from
// the status object. If the condition is not already present, the returned
// status object is returned unchanged. RemoveCondition does not update the
// resource in the cluster.
func (s *HelmAppStatus) RemoveCondition(conditionType HelmAppConditionType) *HelmAppStatus {
for i := range s.Conditions {
if s.Conditions[i].Type == conditionType {
s.Conditions = append(s.Conditions[:i], s.Conditions[i+1:]...)
return s
}
}
return s
}
// StatusFor safely returns a typed status block from a custom resource.
func StatusFor(cr *unstructured.Unstructured) *HelmAppStatus {
switch s := cr.Object["status"].(type) {
case *HelmAppStatus:
return s
case map[string]interface{}:
var status *HelmAppStatus
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(s, &status); err != nil {
return &HelmAppStatus{}
}
return status
default:
return &HelmAppStatus{}
}
}