|
| 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 v1beta1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/open-feature/open-feature-operator/apis/core/v1beta1/common" |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | +) |
| 26 | + |
| 27 | +// FeatureFlagInProcessSourceSpec defines the desired state of FeatureFlagInProcessSource |
| 28 | +type FeatureFlagInProcessSourceSpec struct { |
| 29 | + // Port defines the port to listen on, defaults to 8013 |
| 30 | + // +kubebuilder:default:=8013 |
| 31 | + // +optional |
| 32 | + Port int32 `json:"port"` |
| 33 | + |
| 34 | + // SocketPath defines the unix socket path to listen on |
| 35 | + // +optional |
| 36 | + SocketPath string `json:"socketPath"` |
| 37 | + |
| 38 | + // Host |
| 39 | + // +kubebuilder:default:=localhost |
| 40 | + // +optional |
| 41 | + Host string `json:"host"` |
| 42 | + |
| 43 | + // TLS |
| 44 | + // +kubebuilder:default:=false |
| 45 | + // +optional |
| 46 | + TLS bool `json:"tls"` |
| 47 | + |
| 48 | + // OfflineFlagSourcePath |
| 49 | + // +optional |
| 50 | + OfflineFlagSourcePath string `json:"offlineFlagSourcePath"` |
| 51 | + |
| 52 | + // Selector |
| 53 | + // +optional |
| 54 | + Selector string `json:"selector"` |
| 55 | + |
| 56 | + // Cache |
| 57 | + // +kubebuilder:default:=lru |
| 58 | + // +kubebuilder:validation:Enum:=lru;disabled |
| 59 | + // +optional |
| 60 | + Cache string `json:"cache"` |
| 61 | + |
| 62 | + // CacheMaxSize |
| 63 | + // +kubebuilder:default:=1000 |
| 64 | + // +optional |
| 65 | + CacheMaxSize int `json:"cacheMaxSize"` |
| 66 | + |
| 67 | + //EnvVars |
| 68 | + // +optional |
| 69 | + EnvVars []corev1.EnvVar `json:"envVars"` |
| 70 | + |
| 71 | + // EnvVarPrefix defines the prefix to be applied to all environment variables applied to the sidecar, default FLAGD |
| 72 | + // +optional |
| 73 | + // +kubebuilder:default:=FLAGD |
| 74 | + EnvVarPrefix string `json:"envVarPrefix"` |
| 75 | +} |
| 76 | + |
| 77 | +// FeatureFlagInProcessSourceStatus defines the observed state of FeatureFlagInProcessSource |
| 78 | +type FeatureFlagInProcessSourceStatus struct { |
| 79 | +} |
| 80 | + |
| 81 | +//+kubebuilder:object:root=true |
| 82 | +//+kubebuilder:subresource:status |
| 83 | + |
| 84 | +// FeatureFlagInProcessSource is the Schema for the featureflaginprocesssources API |
| 85 | +type FeatureFlagInProcessSource struct { |
| 86 | + metav1.TypeMeta `json:",inline"` |
| 87 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 88 | + |
| 89 | + Spec FeatureFlagInProcessSourceSpec `json:"spec,omitempty"` |
| 90 | + Status FeatureFlagInProcessSourceStatus `json:"status,omitempty"` |
| 91 | +} |
| 92 | + |
| 93 | +//+kubebuilder:object:root=true |
| 94 | + |
| 95 | +// FeatureFlagInProcessSourceList contains a list of FeatureFlagInProcessSource |
| 96 | +type FeatureFlagInProcessSourceList struct { |
| 97 | + metav1.TypeMeta `json:",inline"` |
| 98 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 99 | + Items []FeatureFlagInProcessSource `json:"items"` |
| 100 | +} |
| 101 | + |
| 102 | +func init() { |
| 103 | + SchemeBuilder.Register(&FeatureFlagInProcessSource{}, &FeatureFlagInProcessSourceList{}) |
| 104 | +} |
| 105 | + |
| 106 | +func (fc *FeatureFlagInProcessSourceSpec) Merge(new *FeatureFlagInProcessSourceSpec) { |
| 107 | + if new == nil { |
| 108 | + return |
| 109 | + } |
| 110 | + if len(new.EnvVars) != 0 { |
| 111 | + fc.EnvVars = append(fc.EnvVars, new.EnvVars...) |
| 112 | + } |
| 113 | + if new.Port != common.DefaultPort { |
| 114 | + fc.Port = new.Port |
| 115 | + } |
| 116 | + if new.SocketPath != "" { |
| 117 | + fc.SocketPath = new.SocketPath |
| 118 | + } |
| 119 | + if new.Host != common.DefaultHost { |
| 120 | + fc.Host = new.Host |
| 121 | + } |
| 122 | + if new.EnvVarPrefix != common.DefaultEnvVarPrefix { |
| 123 | + fc.EnvVarPrefix = new.EnvVarPrefix |
| 124 | + } |
| 125 | + if new.OfflineFlagSourcePath != "" { |
| 126 | + fc.OfflineFlagSourcePath = new.OfflineFlagSourcePath |
| 127 | + } |
| 128 | + if new.Selector != "" { |
| 129 | + fc.Selector = new.Selector |
| 130 | + } |
| 131 | + if new.Cache != common.DefaultCache { |
| 132 | + fc.Cache = new.Cache |
| 133 | + } |
| 134 | + if new.CacheMaxSize != int(common.DefaultCacheMaxSize) { |
| 135 | + fc.CacheMaxSize = new.CacheMaxSize |
| 136 | + } |
| 137 | + if new.TLS != common.DefaultTLS { |
| 138 | + fc.TLS = new.TLS |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func (fc *FeatureFlagInProcessSourceSpec) ToEnvVars() []corev1.EnvVar { |
| 143 | + envs := []corev1.EnvVar{} |
| 144 | + |
| 145 | + for _, envVar := range fc.EnvVars { |
| 146 | + envs = append(envs, corev1.EnvVar{ |
| 147 | + Name: common.EnvVarKey(fc.EnvVarPrefix, envVar.Name), |
| 148 | + Value: envVar.Value, |
| 149 | + }) |
| 150 | + } |
| 151 | + |
| 152 | + envs = append(envs, corev1.EnvVar{ |
| 153 | + Name: common.EnvVarKey(fc.EnvVarPrefix, common.HostEnvVar), |
| 154 | + Value: fc.Host, |
| 155 | + }) |
| 156 | + |
| 157 | + envs = append(envs, corev1.EnvVar{ |
| 158 | + Name: common.EnvVarKey(fc.EnvVarPrefix, common.PortEnvVar), |
| 159 | + Value: fmt.Sprintf("%d", fc.Port), |
| 160 | + }) |
| 161 | + |
| 162 | + envs = append(envs, corev1.EnvVar{ |
| 163 | + Name: common.EnvVarKey(fc.EnvVarPrefix, common.TLSEnvVar), |
| 164 | + Value: fmt.Sprintf("%t", fc.TLS), |
| 165 | + }) |
| 166 | + |
| 167 | + envs = append(envs, corev1.EnvVar{ |
| 168 | + Name: common.EnvVarKey(fc.EnvVarPrefix, common.SocketPathEnvVar), |
| 169 | + Value: fc.SocketPath, |
| 170 | + }) |
| 171 | + |
| 172 | + envs = append(envs, corev1.EnvVar{ |
| 173 | + Name: common.EnvVarKey(fc.EnvVarPrefix, common.OfflineFlagSourcePathEnvVar), |
| 174 | + Value: fc.OfflineFlagSourcePath, |
| 175 | + }) |
| 176 | + |
| 177 | + envs = append(envs, corev1.EnvVar{ |
| 178 | + Name: common.EnvVarKey(fc.EnvVarPrefix, common.SelectorEnvVar), |
| 179 | + Value: fc.Selector, |
| 180 | + }) |
| 181 | + |
| 182 | + envs = append(envs, corev1.EnvVar{ |
| 183 | + Name: common.EnvVarKey(fc.EnvVarPrefix, common.CacheEnvVar), |
| 184 | + Value: fc.Cache, |
| 185 | + }) |
| 186 | + |
| 187 | + envs = append(envs, corev1.EnvVar{ |
| 188 | + Name: common.EnvVarKey(fc.EnvVarPrefix, common.CacheMaxSizeEnvVar), |
| 189 | + Value: fmt.Sprintf("%d", fc.CacheMaxSize), |
| 190 | + }) |
| 191 | + |
| 192 | + return envs |
| 193 | +} |
0 commit comments