-
Notifications
You must be signed in to change notification settings - Fork 6
/
classifier_types.go
191 lines (153 loc) · 6.84 KB
/
classifier_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
Copyright 2022-23. projectsveltos.io. All rights reserved.
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 v1alpha1
import (
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
// ClassifierFinalizer allows ClassifierReconciler to clean up resources associated with
// Classifier before removing it from the apiserver.
ClassifierFinalizer = "classifierfinalizer.projectsveltos.io"
// ClassifierReportClusterNameLabel is added to each ClassifierReport
ClassifierReportClusterNameLabel = "classifier.projectsveltos.io/cluster-name"
// ClassifierReportClusterTypeLabel is added to each ClassifierReport
ClassifierReportClusterTypeLabel = "classifier.projectsveltos.io/cluster-type"
ClassifierKind = "Classifier"
FeatureClassifier = "Classifier"
)
func GetClassifierReportName(classifierName, clusterName string, clusterType *ClusterType) string {
// TODO: shorten this
return fmt.Sprintf("%s--%s--%s",
strings.ToLower(string(*clusterType)), classifierName, clusterName)
}
func GetClassifierReportLabels(classifierName, clusterName string, clusterType *ClusterType) map[string]string {
return map[string]string{
ClassifierlNameLabel: classifierName,
ClassifierReportClusterNameLabel: clusterName,
ClassifierReportClusterTypeLabel: strings.ToLower(string(*clusterType)),
}
}
func GetClusterInfo(clusterNamespace, clusterName string) string {
return fmt.Sprintf("%s--%s", clusterNamespace, clusterName)
}
type ClassifierLabel struct {
// Key is the label key
Key string `json:"key"`
// Value is the label value
Value string `json:"value"`
}
type DeployedResourceConstraint struct {
// ResourceSelectors identifies what resources to select
// If no AggregatedClassification is specified, a cluster is
// a match for Classifier instance, if all ResourceSelectors returns at
// least one match.
ResourceSelectors []ResourceSelector `json:"resourceSelectors"`
// AggregatedClassification is optional and can be used to specify a Lua function
// that will be used to further detect whether the subset of the resources
// selected using the ResourceSelector field are a match for this Classifier.
// The function will receive the array of resources selected by ResourceSelectors.
// If this field is not specified, a cluster is a match for Classifier instance,
// if all ResourceSelectors returns at least one match.
// This field allows to perform more complex evaluation on the resources, looking
// at all resources together.
// This can be useful for more sophisticated tasks, such as identifying resources
// that are related to each other or that have similar properties.
// The Lua function must return a struct with:
// - "matching" field: boolean indicating whether cluster is a match;
// - "message" field: (optional) message.
// +optional
AggregatedClassification string `json:"aggregatedClassification,omitempty"`
}
type KubernetesComparison string
// Define the Action constants.
const (
ComparisonEqual KubernetesComparison = "Equal"
ComparisonNotEqual KubernetesComparison = "NotEqual"
ComparisonGreaterThan KubernetesComparison = "GreaterThan"
ComparisonLessThan KubernetesComparison = "LessThan"
ComparisonGreaterThanOrEqualTo KubernetesComparison = "GreaterThanOrEqualTo"
ComparisonLessThanOrEqualTo KubernetesComparison = "LessThanOrEqualTo"
)
type KubernetesVersionConstraint struct {
// Version is the kubernetes version
Version string `json:"version"`
// Comparison indicate how to compare cluster kubernetes version with the specified version
// +kubebuilder:validation:Enum=Equal;NotEqual;GreaterThan;LessThan;GreaterThanOrEqualTo;LessThanOrEqualTo
Comparison string `json:"comparison"`
}
// ClassifierSpec defines the desired state of Classifier
type ClassifierSpec struct {
// DeployedResourceConstraint allows to classify based on current deployed resources
// +optional
DeployedResourceConstraint *DeployedResourceConstraint `json:"deployedResourceConstraint,omitempty"`
// KubernetesVersionConstraints allows to classify based on current kubernetes version
// +optional
KubernetesVersionConstraints []KubernetesVersionConstraint `json:"kubernetesVersionConstraints,omitempty"`
// ClassifierLabels is set of labels, key,value pair, that will be added to each
// cluster matching Classifier instance
ClassifierLabels []ClassifierLabel `json:"classifierLabels"`
}
type UnManagedLabel struct {
// Key represents a label Classifier would like to manage
// but cannot because currently managed by different instance
Key string `json:"key"`
// FailureMessage is a human consumable message explaining the
// misconfiguration
// +optional
FailureMessage *string `json:"failureMessage,omitempty"`
}
type MachingClusterStatus struct {
// ClusterRef references the matching Cluster
ClusterRef corev1.ObjectReference `json:"clusterRef"`
// ManagedLabels indicates the labels being managed on
// the cluster by this Classifier instance
// +optional
ManagedLabels []string `json:"managedLabels,omitempty"`
// UnManagedLabel indicates the labels this Classifier instance
// would like to manage but cannot because different instance is
// already managing it
// +optional
UnManagedLabels []UnManagedLabel `json:"unManagedLabels,omitempty"`
}
// ClassifierStatus defines the observed state of Classifier
type ClassifierStatus struct {
// MatchingClusterRefs reference all the cluster-api Cluster currently matching
// Classifier
MachingClusterStatuses []MachingClusterStatus `json:"machingClusterStatuses,omitempty"`
// ClusterInfo reference all the cluster-api Cluster where Classifier
// has been/is being deployed
ClusterInfo []ClusterInfo `json:"clusterInfo,omitempty"`
}
//+kubebuilder:object:root=true
//+kubebuilder:resource:path=classifiers,scope=Cluster
//+kubebuilder:subresource:status
// Classifier is the Schema for the classifiers API
type Classifier struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec ClassifierSpec `json:"spec,omitempty"`
Status ClassifierStatus `json:"status,omitempty"`
}
//+kubebuilder:object:root=true
// ClassifierList contains a list of Classifier
type ClassifierList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Classifier `json:"items"`
}
func init() {
SchemeBuilder.Register(&Classifier{}, &ClassifierList{})
}