-
Notifications
You must be signed in to change notification settings - Fork 4
/
application_types.go
163 lines (136 loc) · 5.82 KB
/
application_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2022 Expedia Group
//
// 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 v1beta1
import (
"fmt"
"strings"
"github.com/ExpediaGroup/overwhelm/analyzer"
"github.com/fluxcd/helm-controller/api/v2beta2"
"github.com/fluxcd/pkg/apis/meta"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
const PodReady = "PodReady"
const ContainersNotReady = "ContainersNotReady"
const PodInitializing = "PodInitializing"
type Metadata struct {
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
}
type ReleaseTemplate struct {
// Metadata to be applied to the resources created by the Application Controller
// +optional
Metadata `json:"metadata,omitempty"`
// Spec to be applied to the Helm Release resource created by the Application Controller
// +required
Spec v2beta2.HelmReleaseSpec `json:"spec,omitempty"`
}
// ApplicationSpec defines the desired state of Application
type ApplicationSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
// Data to be consolidated for the Helm Chart's values.yaml file
// +optional
Data map[string]string `json:"data,omitempty"`
// PreRenderer holds custom templating delimiters and a flag.
// By default, standard delimiters "{{" and "}}" will be used to render values within. If specified then the custom delimiters will be used.
// +optional
PreRenderer PreRenderer `json:"preRenderer,omitempty"`
// Template of Release metadata and spec needed for the resources created by the Application Controller
// +required
Template ReleaseTemplate `json:"template,omitempty"`
}
// ApplicationStatus defines the observed state of Application
type ApplicationStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
// ObservedGeneration is the last observed generation.
// +optional
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
// Conditions holds the conditions for the Application.
// +optional
Conditions []metav1.Condition `json:"conditions,omitempty"`
// HelmReleaseGeneration is the helm release generation number
// +optional
HelmReleaseGeneration int64 `json:"helmReleaseGeneration,omitempty"`
// ValuesCheckSum is the checksum of the values file associated with the helm chart
// +optional
ValuesCheckSum string `json:"valuesCheckSum,omitempty"`
// Failures is the reconciliation failure count against the latest desired
// state. It is reset after a successful reconciliation.
// +optional
Failures int64 `json:"failures,omitempty"`
}
// +genclient
// +genclient:Namespaced
// +kubebuilder:object:root=true
// +kubebuilder:resource:shortName=app
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].status",description=""
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].message",description=""
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description=""
//+kubebuilder:storageversion
// Application is the Schema for the applications API
type Application struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec ApplicationSpec `json:"spec,omitempty"`
Status ApplicationStatus `json:"status,omitempty"`
}
//+kubebuilder:object:root=true
// ApplicationList contains a list of Application
type ApplicationList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Application `json:"items"`
}
func init() {
SchemeBuilder.Register(&Application{}, &ApplicationList{})
}
func AppInProgressStatus(application *Application) {
application.Status.Conditions = []metav1.Condition{}
condition := metav1.Condition{
Type: meta.ReadyCondition,
Status: metav1.ConditionUnknown,
Reason: meta.ProgressingReason,
Message: "Reconciliation in progress",
}
apimeta.SetStatusCondition(&application.Status.Conditions, condition)
}
func AppErrorStatus(application *Application, error string) {
condition := metav1.Condition{
Type: meta.ReadyCondition,
Status: metav1.ConditionFalse,
Reason: meta.FailedReason,
Message: error,
}
apimeta.SetStatusCondition(&application.Status.Conditions, condition)
}
func AppPodAnalysisCondition(application *Application, result analyzer.Result) bool {
appPodReadyCondition := apimeta.FindStatusCondition(application.Status.Conditions, PodReady)
condition := metav1.Condition{
Type: PodReady, // Could be meta.ReadyCondition, but it would clash with the HR
}
if !result.Healthy {
condition.Message = fmt.Sprintf("%s %s is unhealthy: %v", result.ResourceType, result.ResourceName, result.Errors)
if appPodReadyCondition == nil || strings.Contains(appPodReadyCondition.Message, PodInitializing) || !strings.Contains(condition.Message, PodInitializing) {
condition.Reason = ContainersNotReady
condition.Status = metav1.ConditionFalse
apimeta.SetStatusCondition(&application.Status.Conditions, condition)
return true
}
}
return false
}