@@ -46,18 +46,8 @@ type DeploymentController struct {
4646
4747// Initialize creates the primary deployment if it does not exist.
4848func (c * DeploymentController ) Initialize (cd * flaggerv1.Canary ) (bool , error ) {
49- targetName := cd .Spec .TargetRef .Name
50- primaryName := fmt .Sprintf ("%s-primary" , cd .Spec .TargetRef .Name )
51-
52- canaryDep , err := c .kubeClient .AppsV1 ().Deployments (cd .Namespace ).Get (context .TODO (), targetName , metav1.GetOptions {})
53- if err != nil {
54- return true , fmt .Errorf ("deployment %s.%s get query error: %w" , targetName , cd .Namespace , err )
55- }
56-
57- if _ , err := c .kubeClient .AppsV1 ().Deployments (cd .Namespace ).Get (context .TODO (), primaryName , metav1.GetOptions {}); errors .IsNotFound (err ) {
58- if err := c .createPrimaryDeployment (cd , canaryDep , primaryName , c .includeLabelPrefix ); err != nil {
59- return true , fmt .Errorf ("createPrimaryDeployment failed: %w" , err )
60- }
49+ if err := c .createPrimaryDeployment (cd , c .includeLabelPrefix ); err != nil {
50+ return true , fmt .Errorf ("createPrimaryDeployment failed: %w" , err )
6151 }
6252
6353 if cd .Status .Phase == "" || cd .Status .Phase == flaggerv1 .CanaryPhaseInitializing {
@@ -249,23 +239,13 @@ func (c *DeploymentController) GetMetadata(cd *flaggerv1.Canary) (map[string]str
249239
250240 return matchLabels , label , labelValue , ports , nil
251241}
252- func (c * DeploymentController ) createPrimaryDeployment (cd * flaggerv1.Canary , canaryDep * appsv1.Deployment , name string , includeLabelPrefix []string ) error {
253- // create primary secrets and config maps
254- configRefs , err := c .configTracker .GetTargetConfigs (cd )
255- if err != nil {
256- return fmt .Errorf ("GetTargetConfigs failed: %w" , err )
257- }
258- if err := c .configTracker .CreatePrimaryConfigs (cd , configRefs , c .includeLabelPrefix ); err != nil {
259- return fmt .Errorf ("CreatePrimaryConfigs failed: %w" , err )
260- }
261- annotations , err := makeAnnotations (canaryDep .Spec .Template .Annotations )
262- if err != nil {
263- return fmt .Errorf ("makeAnnotations failed: %w" , err )
264- }
242+ func (c * DeploymentController ) createPrimaryDeployment (cd * flaggerv1.Canary , includeLabelPrefix []string ) error {
243+ targetName := cd .Spec .TargetRef .Name
244+ primaryName := fmt .Sprintf ("%s-primary" , cd .Spec .TargetRef .Name )
265245
266- replicas := int32 ( 1 )
267- if canaryDep . Spec . Replicas != nil && * canaryDep . Spec . Replicas > 0 {
268- replicas = * canaryDep . Spec . Replicas
246+ canaryDep , err := c . kubeClient . AppsV1 (). Deployments ( cd . Namespace ). Get ( context . TODO (), targetName , metav1. GetOptions {} )
247+ if err != nil {
248+ return fmt . Errorf ( "deployment %s.%s get query error: %w" , targetName , cd . Namespace , err )
269249 }
270250
271251 // Create the labels map but filter unwanted labels
@@ -279,48 +259,69 @@ func (c *DeploymentController) createPrimaryDeployment(cd *flaggerv1.Canary, can
279259
280260 matchLabels [label ] = primaryLabelValue
281261
282- // create primary deployment
283- primaryDep := & appsv1.Deployment {
284- ObjectMeta : metav1.ObjectMeta {
285- Name : name ,
286- Namespace : cd .Namespace ,
287- Labels : makePrimaryLabels (labels , primaryLabelValue , label ),
288- Annotations : filterMetadata (canaryDep .Annotations ),
289- OwnerReferences : []metav1.OwnerReference {
290- * metav1 .NewControllerRef (cd , schema.GroupVersionKind {
291- Group : flaggerv1 .SchemeGroupVersion .Group ,
292- Version : flaggerv1 .SchemeGroupVersion .Version ,
293- Kind : flaggerv1 .CanaryKind ,
294- }),
295- },
296- },
297- Spec : appsv1.DeploymentSpec {
298- ProgressDeadlineSeconds : canaryDep .Spec .ProgressDeadlineSeconds ,
299- MinReadySeconds : canaryDep .Spec .MinReadySeconds ,
300- RevisionHistoryLimit : canaryDep .Spec .RevisionHistoryLimit ,
301- Replicas : int32p (replicas ),
302- Strategy : canaryDep .Spec .Strategy ,
303- Selector : & metav1.LabelSelector {
304- MatchLabels : matchLabels ,
262+ primaryDep , err := c .kubeClient .AppsV1 ().Deployments (cd .Namespace ).Get (context .TODO (), primaryName , metav1.GetOptions {})
263+ if errors .IsNotFound (err ) {
264+ // create primary secrets and config maps
265+ configRefs , err := c .configTracker .GetTargetConfigs (cd )
266+ if err != nil {
267+ return fmt .Errorf ("GetTargetConfigs failed: %w" , err )
268+ }
269+ if err := c .configTracker .CreatePrimaryConfigs (cd , configRefs , c .includeLabelPrefix ); err != nil {
270+ return fmt .Errorf ("CreatePrimaryConfigs failed: %w" , err )
271+ }
272+ annotations , err := makeAnnotations (canaryDep .Spec .Template .Annotations )
273+ if err != nil {
274+ return fmt .Errorf ("makeAnnotations failed: %w" , err )
275+ }
276+
277+ replicas := int32 (1 )
278+ if canaryDep .Spec .Replicas != nil && * canaryDep .Spec .Replicas > 0 {
279+ replicas = * canaryDep .Spec .Replicas
280+ }
281+
282+ // create primary deployment
283+ primaryDep = & appsv1.Deployment {
284+ ObjectMeta : metav1.ObjectMeta {
285+ Name : primaryName ,
286+ Namespace : cd .Namespace ,
287+ Labels : makePrimaryLabels (labels , primaryLabelValue , label ),
288+ Annotations : filterMetadata (canaryDep .Annotations ),
289+ OwnerReferences : []metav1.OwnerReference {
290+ * metav1 .NewControllerRef (cd , schema.GroupVersionKind {
291+ Group : flaggerv1 .SchemeGroupVersion .Group ,
292+ Version : flaggerv1 .SchemeGroupVersion .Version ,
293+ Kind : flaggerv1 .CanaryKind ,
294+ }),
295+ },
305296 },
306- Template : corev1.PodTemplateSpec {
307- ObjectMeta : metav1.ObjectMeta {
308- Labels : makePrimaryLabels (canaryDep .Spec .Template .Labels , primaryLabelValue , label ),
309- Annotations : annotations ,
297+ Spec : appsv1.DeploymentSpec {
298+ ProgressDeadlineSeconds : canaryDep .Spec .ProgressDeadlineSeconds ,
299+ MinReadySeconds : canaryDep .Spec .MinReadySeconds ,
300+ RevisionHistoryLimit : canaryDep .Spec .RevisionHistoryLimit ,
301+ Replicas : int32p (replicas ),
302+ Strategy : canaryDep .Spec .Strategy ,
303+ Selector : & metav1.LabelSelector {
304+ MatchLabels : matchLabels ,
305+ },
306+ Template : corev1.PodTemplateSpec {
307+ ObjectMeta : metav1.ObjectMeta {
308+ Labels : makePrimaryLabels (canaryDep .Spec .Template .Labels , primaryLabelValue , label ),
309+ Annotations : annotations ,
310+ },
311+ // update spec with the primary secrets and config maps
312+ Spec : c .getPrimaryDeploymentTemplateSpec (canaryDep , configRefs ),
310313 },
311- // update spec with the primary secrets and config maps
312- Spec : c .getPrimaryDeploymentTemplateSpec (canaryDep , configRefs ),
313314 },
314- },
315- }
315+ }
316316
317- _ , err = c .kubeClient .AppsV1 ().Deployments (cd .Namespace ).Create (context .TODO (), primaryDep , metav1.CreateOptions {})
318- if err != nil {
319- return fmt .Errorf ("creating deployment %s.%s failed: %w" , primaryDep .Name , cd .Namespace , err )
320- }
317+ _ , err = c .kubeClient .AppsV1 ().Deployments (cd .Namespace ).Create (context .TODO (), primaryDep , metav1.CreateOptions {})
318+ if err != nil {
319+ return fmt .Errorf ("creating deployment %s.%s failed: %w" , primaryDep .Name , cd .Namespace , err )
320+ }
321321
322- c .logger .With ("canary" , fmt .Sprintf ("%s.%s" , cd .Name , cd .Namespace )).
323- Infof ("Deployment %s.%s created" , primaryDep .GetName (), cd .Namespace )
322+ c .logger .With ("canary" , fmt .Sprintf ("%s.%s" , cd .Name , cd .Namespace )).
323+ Infof ("Deployment %s.%s created" , primaryDep .GetName (), cd .Namespace )
324+ }
324325
325326 return nil
326327}
0 commit comments