github.com/openebs/api@v1.12.0/pkg/kubernetes/apps/deployment.go (about) 1 // Copyright © 2020 The OpenEBS Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package apps 16 17 import ( 18 corebuilder "github.com/openebs/api/pkg/kubernetes/core" 19 appsv1 "k8s.io/api/apps/v1" 20 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 21 ) 22 23 type Deployment struct { 24 *appsv1.Deployment 25 } 26 27 // NewDeployment returns an empty instance of deployment 28 func NewDeployment() *Deployment { 29 return &Deployment{ 30 &appsv1.Deployment{}, 31 } 32 } 33 34 // WithName sets the Name field of deployment with provided value. 35 func (d *Deployment) WithName(name string) *Deployment { 36 d.Name = name 37 return d 38 } 39 40 // WithNamespace sets the Namespace field of deployment with provided value. 41 func (d *Deployment) WithNamespace(namespace string) *Deployment { 42 d.Namespace = namespace 43 return d 44 } 45 46 // WithAnnotations merges existing annotations if any 47 // with the ones that are provided here 48 func (d *Deployment) WithAnnotations(annotations map[string]string) *Deployment { 49 if d.Annotations == nil { 50 return d.WithAnnotationsNew(annotations) 51 } 52 53 for key, value := range annotations { 54 d.Annotations[key] = value 55 } 56 return d 57 } 58 59 // WithAnnotationsNew resets existing annotaions if any with 60 // ones that are provided here 61 func (d *Deployment) WithAnnotationsNew(annotations map[string]string) *Deployment { 62 newannotations := make(map[string]string) 63 for key, value := range annotations { 64 newannotations[key] = value 65 } 66 67 d.Annotations = newannotations 68 return d 69 } 70 71 // WithNodeSelector Sets the node selector with the provided argument. 72 func (d *Deployment) WithNodeSelector(selector map[string]string) *Deployment { 73 if d.Spec.Template.Spec.NodeSelector == nil { 74 return d.WithNodeSelectorNew(selector) 75 } 76 77 for key, value := range selector { 78 d.Spec.Template.Spec.NodeSelector[key] = value 79 } 80 return d 81 } 82 83 // WithNodeSelector Sets the node selector with the provided argument. 84 func (d *Deployment) WithNodeSelectorNew(selector map[string]string) *Deployment { 85 d.Spec.Template.Spec.NodeSelector = selector 86 return d 87 } 88 89 // WithOwnerReferenceNew sets ownerreference if any with 90 // ones that are provided here 91 func (d *Deployment) WithOwnerReferenceNew(ownerRefernce []metav1.OwnerReference) *Deployment { 92 d.OwnerReferences = ownerRefernce 93 return d 94 } 95 96 // WithLabels merges existing labels if any 97 // with the ones that are provided here 98 func (d *Deployment) WithLabels(labels map[string]string) *Deployment { 99 if d.Labels == nil { 100 return d.WithLabelsNew(labels) 101 } 102 103 for key, value := range labels { 104 d.Labels[key] = value 105 } 106 return d 107 } 108 109 // WithLabelsNew resets existing labels if any with 110 // ones that are provided here 111 func (d *Deployment) WithLabelsNew(labels map[string]string) *Deployment { 112 newLabels := make(map[string]string) 113 for key, value := range labels { 114 newLabels[key] = value 115 } 116 117 d.Labels = newLabels 118 return d 119 } 120 121 // WithSelectorMatchLabels merges existing matchlabels if any 122 // with the ones that are provided here 123 func (d *Deployment) WithSelectorMatchLabels(matchlabels map[string]string) *Deployment { 124 if d.Spec.Selector == nil { 125 return d.WithSelectorMatchLabelsNew(matchlabels) 126 } 127 128 for key, value := range matchlabels { 129 d.Spec.Selector.MatchLabels[key] = value 130 } 131 return d 132 } 133 134 // WithSelectorMatchLabelsNew resets existing matchlabels if any with 135 // ones that are provided here 136 func (d *Deployment) WithSelectorMatchLabelsNew(matchlabels map[string]string) *Deployment { 137 newmatchlabels := make(map[string]string) 138 for key, value := range matchlabels { 139 newmatchlabels[key] = value 140 } 141 142 newselector := &metav1.LabelSelector{ 143 MatchLabels: newmatchlabels, 144 } 145 146 d.Spec.Selector = newselector 147 return d 148 } 149 150 // WithReplicas sets the replica field of deployment. 151 // Caller should not pass nil value. 152 func (d *Deployment) WithReplicas(replicas *int32) *Deployment { 153 newreplicas := *replicas 154 d.Spec.Replicas = &newreplicas 155 return d 156 } 157 158 //WithStrategyType sets the strategy field of the deployment 159 func (d *Deployment) WithStrategyType(strategytype appsv1.DeploymentStrategyType) *Deployment { 160 d.Spec.Strategy.Type = strategytype 161 return d 162 } 163 164 // WithPodTemplateSpecBuilder sets the template field of the deployment 165 func (d *Deployment) WithPodTemplateSpec(pts *corebuilder.PodTemplateSpec) *Deployment { 166 templatespecObj := pts.Build() 167 d.Spec.Template = *templatespecObj 168 return d 169 } 170 171 // Build returns a deployment instance 172 func (d *Deployment) Build() *appsv1.Deployment { 173 return d.Deployment 174 }