|
| 1 | +/* |
| 2 | +Copyright 2022. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1alpha1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + FlagdMetricPortEnvVar string = "FLAGD_METRICS_PORT" |
| 29 | + FlagdPortEnvVar string = "FLAGD_PORT" |
| 30 | + FlagdSocketPathEnvVar string = "FLAGD_SOCKET_PATH" |
| 31 | + FlagdEvaluatorEnvVar string = "FLAGD_EVALUATOR" |
| 32 | + flagDVersionEnvVar string = "FLAGD_VERSION" |
| 33 | + defaultMetricPort int32 = 8014 |
| 34 | + defaultPort int32 = 8013 |
| 35 | + defaultSocketPath string = "" |
| 36 | + defaultEvaluator string = "json" |
| 37 | + defaultImage string = "ghcr.io/open-feature/flagd" |
| 38 | + defaultTag string = "main" |
| 39 | +) |
| 40 | + |
| 41 | +// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! |
| 42 | +// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. |
| 43 | + |
| 44 | +// FlagSourceConfigurationSpec defines the desired state of FlagSourceConfiguration |
| 45 | +type FlagSourceConfigurationSpec struct { |
| 46 | + // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster |
| 47 | + // Important: Run "make" to regenerate code after modifying this file |
| 48 | + |
| 49 | + // MetricsPort defines the port to serve metrics on, defaults to 8014 |
| 50 | + // +optional |
| 51 | + MetricsPort int32 `json:"metricsPort"` |
| 52 | + |
| 53 | + // Port defines the port to listen on, defaults to 8013 |
| 54 | + // +optional |
| 55 | + Port int32 `json:"port"` |
| 56 | + |
| 57 | + // SocketPath defines the unix socket path to listen on |
| 58 | + // +optional |
| 59 | + SocketPath string `json:"socketPath"` |
| 60 | + |
| 61 | + //SyncProviderArgs are string arguments passed to all sync providers, defined as key values separated by = |
| 62 | + // +optional |
| 63 | + SyncProviderArgs []string `json:"syncProviderArgs"` |
| 64 | + |
| 65 | + // Evaluator sets an evaluator, defaults to 'json' |
| 66 | + // +optional |
| 67 | + Evaluator string `json:"evaluator"` |
| 68 | + |
| 69 | + // Image allows for the flagd image to be overridden, defaults to 'ghcr.io/open-feature/flagd' |
| 70 | + // +optional |
| 71 | + Image string `json:"image"` |
| 72 | + |
| 73 | + // Tag to be appended to the flagd image, defaults to 'main' |
| 74 | + // +optional |
| 75 | + Tag string `json:"tag"` |
| 76 | +} |
| 77 | + |
| 78 | +func NewFlagSourceConfigurationSpec() *FlagSourceConfigurationSpec { |
| 79 | + var tag = defaultTag |
| 80 | + if flagDVersion := os.Getenv(flagDVersionEnvVar); flagDVersion != "" { |
| 81 | + tag = flagDVersion |
| 82 | + } |
| 83 | + return &FlagSourceConfigurationSpec{ |
| 84 | + MetricsPort: defaultMetricPort, |
| 85 | + Port: defaultPort, |
| 86 | + SocketPath: defaultSocketPath, |
| 87 | + SyncProviderArgs: []string{}, |
| 88 | + Evaluator: defaultEvaluator, |
| 89 | + Image: defaultImage, |
| 90 | + Tag: tag, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func (fc *FlagSourceConfigurationSpec) Merge(new *FlagSourceConfigurationSpec) { |
| 95 | + if new == nil { |
| 96 | + return |
| 97 | + } |
| 98 | + if new.MetricsPort != 0 { |
| 99 | + fc.MetricsPort = new.MetricsPort |
| 100 | + } |
| 101 | + if new.Port != 0 { |
| 102 | + fc.Port = new.Port |
| 103 | + } |
| 104 | + if new.SocketPath != "" { |
| 105 | + fc.SocketPath = new.SocketPath |
| 106 | + } |
| 107 | + if new.Evaluator != "" { |
| 108 | + fc.Evaluator = new.Evaluator |
| 109 | + } |
| 110 | + if new.Image != "" { |
| 111 | + fc.Image = new.Image |
| 112 | + } |
| 113 | + if new.Tag != "" { |
| 114 | + fc.Tag = new.Tag |
| 115 | + } |
| 116 | + if new.SyncProviderArgs != nil && len(new.SyncProviderArgs) > 0 { |
| 117 | + fc.SyncProviderArgs = append(fc.SyncProviderArgs, new.SyncProviderArgs...) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func (fc *FlagSourceConfigurationSpec) ToEnvVars() []corev1.EnvVar { |
| 122 | + envs := []corev1.EnvVar{} |
| 123 | + |
| 124 | + if fc.MetricsPort != defaultMetricPort { |
| 125 | + envs = append(envs, corev1.EnvVar{ |
| 126 | + Name: FlagdMetricPortEnvVar, |
| 127 | + Value: fmt.Sprintf("%d", fc.MetricsPort), |
| 128 | + }) |
| 129 | + } |
| 130 | + |
| 131 | + if fc.Port != defaultPort { |
| 132 | + envs = append(envs, corev1.EnvVar{ |
| 133 | + Name: FlagdPortEnvVar, |
| 134 | + Value: fmt.Sprintf("%d", fc.Port), |
| 135 | + }) |
| 136 | + } |
| 137 | + |
| 138 | + if fc.Evaluator != defaultEvaluator { |
| 139 | + envs = append(envs, corev1.EnvVar{ |
| 140 | + Name: FlagdEvaluatorEnvVar, |
| 141 | + Value: fc.Evaluator, |
| 142 | + }) |
| 143 | + } |
| 144 | + |
| 145 | + if fc.SocketPath != defaultSocketPath { |
| 146 | + envs = append(envs, corev1.EnvVar{ |
| 147 | + Name: FlagdSocketPathEnvVar, |
| 148 | + Value: fc.SocketPath, |
| 149 | + }) |
| 150 | + } |
| 151 | + return envs |
| 152 | +} |
| 153 | + |
| 154 | +// FlagSourceConfigurationStatus defines the observed state of FlagSourceConfiguration |
| 155 | +type FlagSourceConfigurationStatus struct { |
| 156 | + // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster |
| 157 | + // Important: Run "make" to regenerate code after modifying this file |
| 158 | +} |
| 159 | + |
| 160 | +//+kubebuilder:resource:shortName="fsc" |
| 161 | +//+kubebuilder:object:root=true |
| 162 | +//+kubebuilder:subresource:status |
| 163 | +//+kubebuilder:storageversion |
| 164 | + |
| 165 | +// FlagSourceConfiguration is the Schema for the FlagSourceConfigurations API |
| 166 | +type FlagSourceConfiguration struct { |
| 167 | + metav1.TypeMeta `json:",inline"` |
| 168 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 169 | + |
| 170 | + Spec FlagSourceConfigurationSpec `json:"spec,omitempty"` |
| 171 | + Status FlagSourceConfigurationStatus `json:"status,omitempty"` |
| 172 | +} |
| 173 | + |
| 174 | +//+kubebuilder:object:root=true |
| 175 | + |
| 176 | +// FlagSourceConfigurationList contains a list of FlagSourceConfiguration |
| 177 | +type FlagSourceConfigurationList struct { |
| 178 | + metav1.TypeMeta `json:",inline"` |
| 179 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 180 | + Items []FlagSourceConfiguration `json:"items"` |
| 181 | +} |
| 182 | + |
| 183 | +func init() { |
| 184 | + SchemeBuilder.Register(&FlagSourceConfiguration{}, &FlagSourceConfigurationList{}) |
| 185 | +} |
0 commit comments