|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package manifest |
| 15 | + |
| 16 | +import ( |
| 17 | + "github.com/aws/amazon-vpc-cni-k8s/test/framework/utils" |
| 18 | + "github.com/aws/aws-sdk-go/aws" |
| 19 | + v1 "k8s.io/api/apps/v1" |
| 20 | + corev1 "k8s.io/api/core/v1" |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | +) |
| 23 | + |
| 24 | +type DaemonsetBuilder struct { |
| 25 | + namespace string |
| 26 | + name string |
| 27 | + container corev1.Container |
| 28 | + labels map[string]string |
| 29 | + nodeSelector map[string]string |
| 30 | + terminationGracePeriod int |
| 31 | + hostNetwork bool |
| 32 | + volume []corev1.Volume |
| 33 | + volumeMount []corev1.VolumeMount |
| 34 | +} |
| 35 | + |
| 36 | +func NewDefaultDaemonsetBuilder() *DaemonsetBuilder { |
| 37 | + return &DaemonsetBuilder{ |
| 38 | + namespace: utils.DefaultTestNamespace, |
| 39 | + terminationGracePeriod: 1, |
| 40 | + labels: map[string]string{"role": "test"}, |
| 41 | + nodeSelector: map[string]string{"kubernetes.io/os": "linux"}, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func (d *DaemonsetBuilder) Labels(labels map[string]string) *DaemonsetBuilder { |
| 46 | + d.labels = labels |
| 47 | + return d |
| 48 | +} |
| 49 | + |
| 50 | +func (d *DaemonsetBuilder) NodeSelector(labelKey string, labelVal string) *DaemonsetBuilder { |
| 51 | + if labelKey != "" { |
| 52 | + d.nodeSelector[labelKey] = labelVal |
| 53 | + } |
| 54 | + return d |
| 55 | +} |
| 56 | + |
| 57 | +func (d *DaemonsetBuilder) Namespace(namespace string) *DaemonsetBuilder { |
| 58 | + d.namespace = namespace |
| 59 | + return d |
| 60 | +} |
| 61 | + |
| 62 | +func (d *DaemonsetBuilder) TerminationGracePeriod(tg int) *DaemonsetBuilder { |
| 63 | + d.terminationGracePeriod = tg |
| 64 | + return d |
| 65 | +} |
| 66 | + |
| 67 | +func (d *DaemonsetBuilder) Name(name string) *DaemonsetBuilder { |
| 68 | + d.name = name |
| 69 | + return d |
| 70 | +} |
| 71 | + |
| 72 | +func (d *DaemonsetBuilder) Container(container corev1.Container) *DaemonsetBuilder { |
| 73 | + d.container = container |
| 74 | + return d |
| 75 | +} |
| 76 | + |
| 77 | +func (d *DaemonsetBuilder) PodLabel(labelKey string, labelValue string) *DaemonsetBuilder { |
| 78 | + d.labels[labelKey] = labelValue |
| 79 | + return d |
| 80 | +} |
| 81 | + |
| 82 | +func (d *DaemonsetBuilder) HostNetwork(hostNetwork bool) *DaemonsetBuilder { |
| 83 | + d.hostNetwork = hostNetwork |
| 84 | + return d |
| 85 | +} |
| 86 | + |
| 87 | +func (d *DaemonsetBuilder) MountVolume(volume []corev1.Volume, volumeMount []corev1.VolumeMount) *DaemonsetBuilder { |
| 88 | + d.volume = volume |
| 89 | + d.volumeMount = volumeMount |
| 90 | + return d |
| 91 | +} |
| 92 | + |
| 93 | +func (d *DaemonsetBuilder) Build() *v1.DaemonSet { |
| 94 | + deploymentSpec := &v1.DaemonSet{ |
| 95 | + ObjectMeta: metav1.ObjectMeta{ |
| 96 | + Name: d.name, |
| 97 | + Namespace: d.namespace, |
| 98 | + Labels: d.labels, |
| 99 | + }, |
| 100 | + Spec: v1.DaemonSetSpec{ |
| 101 | + Selector: &metav1.LabelSelector{ |
| 102 | + MatchLabels: d.labels, |
| 103 | + }, |
| 104 | + Template: corev1.PodTemplateSpec{ |
| 105 | + ObjectMeta: metav1.ObjectMeta{ |
| 106 | + Labels: d.labels, |
| 107 | + }, |
| 108 | + Spec: corev1.PodSpec{ |
| 109 | + HostNetwork: d.hostNetwork, |
| 110 | + NodeSelector: d.nodeSelector, |
| 111 | + Containers: []corev1.Container{d.container}, |
| 112 | + TerminationGracePeriodSeconds: aws.Int64(int64(d.terminationGracePeriod)), |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + if len(d.volume) > 0 && len(d.volumeMount) > 0 { |
| 119 | + deploymentSpec.Spec.Template.Spec.Volumes = d.volume |
| 120 | + deploymentSpec.Spec.Template.Spec.Containers[0].VolumeMounts = d.volumeMount |
| 121 | + } |
| 122 | + return deploymentSpec |
| 123 | +} |
0 commit comments