github.com/openebs/api@v1.12.0/pkg/kubernetes/core/container.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  // container encapsulates kubernetes container type
    22  type Container struct {
    23  	*corev1.Container
    24  }
    25  
    26  // NewContainer returns a new instance of container
    27  func NewContainer() *Container {
    28  	return &Container{
    29  		&corev1.Container{},
    30  	}
    31  }
    32  
    33  // WithName sets the name of the container
    34  func (c *Container) WithName(name string) *Container {
    35  	c.Name = name
    36  	return c
    37  }
    38  
    39  // WithImage sets the image of the container
    40  func (c *Container) WithImage(img string) *Container {
    41  	c.Image = img
    42  	return c
    43  }
    44  
    45  // WithCommandNew sets the command of the container
    46  func (c *Container) WithCommandNew(cmd []string) *Container {
    47  
    48  	newcmd := []string{}
    49  	newcmd = append(newcmd, cmd...)
    50  	c.Command = newcmd
    51  	return c
    52  }
    53  
    54  // WithArgumentsNew sets the command arguments of the container
    55  func (c *Container) WithArgumentsNew(args []string) *Container {
    56  	newargs := []string{}
    57  	newargs = append(newargs, args...)
    58  	c.Args = newargs
    59  	return c
    60  }
    61  
    62  // WithVolumeMountsNew sets the command arguments of the container
    63  func (c *Container) WithVolumeMountsNew(volumeMounts []corev1.VolumeMount) *Container {
    64  	newvolumeMounts := []corev1.VolumeMount{}
    65  	newvolumeMounts = append(newvolumeMounts, volumeMounts...)
    66  	c.VolumeMounts = newvolumeMounts
    67  	return c
    68  }
    69  
    70  // WithVolumeDevicesNew sets the containers with the appropriate volumeDevices
    71  func (c *Container) WithVolumeDevicesNew(volumeDevices []corev1.VolumeDevice) *Container {
    72  	newVolumeDevices := []corev1.VolumeDevice{}
    73  	newVolumeDevices = append(newVolumeDevices, volumeDevices...)
    74  	c.VolumeDevices = newVolumeDevices
    75  	return c
    76  }
    77  
    78  // WithImagePullPolicy sets the image pull policy of the container
    79  func (c *Container) WithImagePullPolicy(policy corev1.PullPolicy) *Container {
    80  	c.ImagePullPolicy = policy
    81  	return c
    82  }
    83  
    84  // WithPrivilegedSecurityContext sets securitycontext of the container
    85  func (c *Container) WithPrivilegedSecurityContext(privileged *bool) *Container {
    86  	newprivileged := *privileged
    87  	newsecuritycontext := &corev1.SecurityContext{
    88  		Privileged: &newprivileged,
    89  	}
    90  
    91  	c.SecurityContext = newsecuritycontext
    92  	return c
    93  }
    94  
    95  // WithResources sets resources of the container
    96  func (c *Container) WithResources(resources corev1.ResourceRequirements) *Container {
    97  	c.Resources = resources
    98  	return c
    99  }
   100  
   101  func (c *Container) WithResourcesByRef(resources *corev1.ResourceRequirements) *Container {
   102  	newresources := *resources
   103  	c.Resources = newresources
   104  	return c
   105  }
   106  
   107  // WithPortsNew sets ports of the container
   108  func (c *Container) WithPortsNew(ports []corev1.ContainerPort) *Container {
   109  	newports := []corev1.ContainerPort{}
   110  	newports = append(newports, ports...)
   111  	c.Ports = newports
   112  	return c
   113  }
   114  
   115  // WithEnvsNew sets the envs of the container
   116  func (c *Container) WithEnvsNew(envs []corev1.EnvVar) *Container {
   117  	newenvs := []corev1.EnvVar{}
   118  	newenvs = append(newenvs, envs...)
   119  	c.Env = newenvs
   120  	return c
   121  }
   122  
   123  // WithEnvs sets the envs of the container
   124  func (c *Container) WithEnvs(envs []corev1.EnvVar) *Container {
   125  	c.Env = append(c.Env, envs...)
   126  	return c
   127  }
   128  
   129  // WithLivenessProbe sets the liveness probe of the container
   130  func (c *Container) WithLivenessProbe(liveness *corev1.Probe) *Container {
   131  	c.LivenessProbe = liveness
   132  	return c
   133  }
   134  
   135  // WithLifeCycle sets the life cycle of the container
   136  func (c *Container) WithLifeCycle(lc *corev1.Lifecycle) *Container {
   137  	c.Lifecycle = lc
   138  	return c
   139  }
   140  
   141  func (c *Container) Build() *corev1.Container {
   142  	return c.Container
   143  }