github.com/openebs/api@v1.12.0/pkg/kubernetes/core/pod.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 core
    16  
    17  import (
    18  	corev1 "k8s.io/api/core/v1"
    19  )
    20  
    21  // PodTemplateSpec holds the api's podtemplatespec objects
    22  type PodTemplateSpec struct {
    23  	*corev1.PodTemplateSpec
    24  }
    25  
    26  // NewPodTemplateSpec returns new instance of PodTemplateSpec
    27  func NewPodTemplateSpec() *PodTemplateSpec {
    28  	return &PodTemplateSpec{
    29  		&corev1.PodTemplateSpec{},
    30  	}
    31  }
    32  
    33  // WithName sets the Name field of podtemplatespec with provided value.
    34  func (p *PodTemplateSpec) WithName(name string) *PodTemplateSpec {
    35  	p.Name = name
    36  	return p
    37  }
    38  
    39  // WithNamespace sets the Namespace field of PodTemplateSpec with provided value.
    40  func (p *PodTemplateSpec) WithNamespace(namespace string) *PodTemplateSpec {
    41  
    42  	p.Namespace = namespace
    43  	return p
    44  }
    45  
    46  // WithAnnotations merges existing annotations if any
    47  // with the ones that are provided here
    48  func (p *PodTemplateSpec) WithAnnotations(annotations map[string]string) *PodTemplateSpec {
    49  	for key, value := range annotations {
    50  		p.Annotations[key] = value
    51  	}
    52  	return p
    53  }
    54  
    55  // WithAnnotationsNew resets the annotation field of podtemplatespec
    56  // with provided arguments
    57  func (p *PodTemplateSpec) WithAnnotationsNew(annotations map[string]string) *PodTemplateSpec {
    58  	newannotations := make(map[string]string)
    59  	for key, value := range annotations {
    60  		newannotations[key] = value
    61  	}
    62  	p.Annotations = newannotations
    63  	return p
    64  }
    65  
    66  // WithLabels merges existing labels if any
    67  // with the ones that are provided here
    68  func (p *PodTemplateSpec) WithLabels(labels map[string]string) *PodTemplateSpec {
    69  	if p.Labels == nil {
    70  		return p.WithLabelsNew(labels)
    71  	}
    72  
    73  	for key, value := range labels {
    74  		p.Labels[key] = value
    75  	}
    76  	return p
    77  }
    78  
    79  // WithLabelsNew resets the labels field of podtemplatespec
    80  // with provided arguments
    81  func (p *PodTemplateSpec) WithLabelsNew(labels map[string]string) *PodTemplateSpec {
    82  	newLabels := make(map[string]string)
    83  	for key, value := range labels {
    84  		newLabels[key] = value
    85  	}
    86  
    87  	p.Labels = newLabels
    88  	return p
    89  }
    90  
    91  // WithNodeSelector merges the nodeselectors if present
    92  // with the provided arguments
    93  func (p *PodTemplateSpec) WithNodeSelector(nodeselectors map[string]string) *PodTemplateSpec {
    94  	if p.Spec.NodeSelector == nil {
    95  		return p.WithNodeSelectorNew(nodeselectors)
    96  	}
    97  
    98  	for key, value := range nodeselectors {
    99  		p.Spec.NodeSelector[key] = value
   100  	}
   101  	return p
   102  }
   103  
   104  // WithPriorityClassName sets the PriorityClassName field of podtemplatespec
   105  func (p *PodTemplateSpec) WithPriorityClassName(prorityClassName string) *PodTemplateSpec {
   106  	p.Spec.PriorityClassName = prorityClassName
   107  	return p
   108  }
   109  
   110  // WithNodeSelectorNew resets the nodeselector field of podtemplatespec
   111  // with provided arguments
   112  func (p *PodTemplateSpec) WithNodeSelectorNew(nodeselectors map[string]string) *PodTemplateSpec {
   113  	newnodeselectors := make(map[string]string)
   114  	for key, value := range nodeselectors {
   115  		newnodeselectors[key] = value
   116  	}
   117  
   118  	p.Spec.NodeSelector = newnodeselectors
   119  	return p
   120  }
   121  
   122  func (p *PodTemplateSpec) WithNodeSelectorByValue(nodeselectors map[string]string) *PodTemplateSpec {
   123  	newnodeselectors := make(map[string]string)
   124  	for key, value := range nodeselectors {
   125  		newnodeselectors[key] = value
   126  	}
   127  
   128  	p.Spec.NodeSelector = newnodeselectors
   129  	return p
   130  }
   131  
   132  // WithServiceAccountName sets the ServiceAccountnNme field of podtemplatespec
   133  func (p *PodTemplateSpec) WithServiceAccountName(serviceAccountnNme string) *PodTemplateSpec {
   134  	p.Spec.ServiceAccountName = serviceAccountnNme
   135  	return p
   136  }
   137  
   138  // WithAffinity sets the affinity field of podtemplatespec
   139  func (p *PodTemplateSpec) WithAffinity(affinity *corev1.Affinity) *PodTemplateSpec {
   140  	p.Spec.Affinity = affinity
   141  	return p
   142  }
   143  
   144  // WithTolerationsByValue sets pod toleration.
   145  // If provided tolerations argument is empty it does not complain.
   146  func (p *PodTemplateSpec) WithTolerations(tolerations ...corev1.Toleration) *PodTemplateSpec {
   147  	p.Spec.Tolerations = tolerations
   148  	return p
   149  }
   150  
   151  // WithTolerationsNew sets the tolerations field of podtemplatespec
   152  func (p *PodTemplateSpec) WithTolerationsNew(tolerations ...corev1.Toleration) *PodTemplateSpec {
   153  	if len(tolerations) == 0 {
   154  		return p.WithTolerations(tolerations...)
   155  	}
   156  
   157  	newtolerations := []corev1.Toleration{}
   158  	newtolerations = append(newtolerations, tolerations...)
   159  
   160  	p.Spec.Tolerations = newtolerations
   161  
   162  	return p
   163  }
   164  
   165  // WithContainers builds the list of containerbuilder
   166  // provided and merges it to the containers field of the podtemplatespec
   167  func (p *PodTemplateSpec) WithContainers(containersList ...*Container) *PodTemplateSpec {
   168  	for _, container := range containersList {
   169  		containerObj := container.Build()
   170  		p.Spec.Containers = append(
   171  			p.Spec.Containers,
   172  			*containerObj,
   173  		)
   174  	}
   175  	return p
   176  }
   177  
   178  // WithVolumes builds the list of volumebuilders provided
   179  // and merges it to the volumes field of podtemplatespec.
   180  func (p *PodTemplateSpec) WithVolumes(volumerList ...*Volume) *PodTemplateSpec {
   181  	for _, volume := range volumerList {
   182  		vol := volume.Build()
   183  		p.Spec.Volumes = append(p.Spec.Volumes, *vol)
   184  	}
   185  	return p
   186  }
   187  
   188  func (p *PodTemplateSpec) Build() *corev1.PodTemplateSpec {
   189  	return p.PodTemplateSpec
   190  }