|
| 1 | +/* |
| 2 | +Copyright 2024 IBM Corporation. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "math/rand" |
| 22 | + "time" |
| 23 | + |
| 24 | + . "github.com/onsi/gomega" |
| 25 | + |
| 26 | + workloadv1beta2 "github.com/project-codeflare/appwrapper/api/v1beta2" |
| 27 | + "k8s.io/apimachinery/pkg/api/resource" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "k8s.io/apimachinery/pkg/runtime" |
| 30 | + "sigs.k8s.io/yaml" |
| 31 | +) |
| 32 | + |
| 33 | +const charset = "abcdefghijklmnopqrstuvwxyz0123456789" |
| 34 | + |
| 35 | +func randName(baseName string) string { |
| 36 | + seededRand := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 37 | + b := make([]byte, 6) |
| 38 | + for i := range b { |
| 39 | + b[i] = charset[seededRand.Intn(len(charset))] |
| 40 | + } |
| 41 | + return fmt.Sprintf("%s-%s", baseName, string(b)) |
| 42 | +} |
| 43 | + |
| 44 | +func toAppWrapper(components ...workloadv1beta2.AppWrapperComponent) *workloadv1beta2.AppWrapper { |
| 45 | + return &workloadv1beta2.AppWrapper{ |
| 46 | + ObjectMeta: metav1.ObjectMeta{Name: randName("aw"), Namespace: "default"}, |
| 47 | + Spec: workloadv1beta2.AppWrapperSpec{Components: components}, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +const podYAML = ` |
| 52 | +apiVersion: v1 |
| 53 | +kind: Pod |
| 54 | +metadata: |
| 55 | + name: %v |
| 56 | +spec: |
| 57 | + restartPolicy: Never |
| 58 | + containers: |
| 59 | + - name: busybox |
| 60 | + image: quay.io/project-codeflare/busybox:1.36 |
| 61 | + command: ["sh", "-c", "sleep 1000"] |
| 62 | + resources: |
| 63 | + requests: |
| 64 | + cpu: %v` |
| 65 | + |
| 66 | +func pod(milliCPU int64) workloadv1beta2.AppWrapperComponent { |
| 67 | + yamlString := fmt.Sprintf(podYAML, |
| 68 | + randName("pod"), |
| 69 | + resource.NewMilliQuantity(milliCPU, resource.DecimalSI)) |
| 70 | + |
| 71 | + jsonBytes, err := yaml.YAMLToJSON([]byte(yamlString)) |
| 72 | + Expect(err).NotTo(HaveOccurred()) |
| 73 | + replicas := int32(1) |
| 74 | + return workloadv1beta2.AppWrapperComponent{ |
| 75 | + PodSets: []workloadv1beta2.AppWrapperPodSet{{Replicas: &replicas, Path: "template"}}, |
| 76 | + Template: runtime.RawExtension{Raw: jsonBytes}, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +const namespacedPodYAML = ` |
| 81 | +apiVersion: v1 |
| 82 | +kind: Pod |
| 83 | +metadata: |
| 84 | + name: %v |
| 85 | + namespace: %v |
| 86 | +spec: |
| 87 | + restartPolicy: Never |
| 88 | + containers: |
| 89 | + - name: busybox |
| 90 | + image: quay.io/project-codeflare/busybox:1.36 |
| 91 | + command: ["sh", "-c", "sleep 1000"] |
| 92 | + resources: |
| 93 | + requests: |
| 94 | + cpu: %v` |
| 95 | + |
| 96 | +func namespacedPod(namespace string, milliCPU int64) workloadv1beta2.AppWrapperComponent { |
| 97 | + yamlString := fmt.Sprintf(namespacedPodYAML, |
| 98 | + randName("pod"), |
| 99 | + namespace, |
| 100 | + resource.NewMilliQuantity(milliCPU, resource.DecimalSI)) |
| 101 | + |
| 102 | + jsonBytes, err := yaml.YAMLToJSON([]byte(yamlString)) |
| 103 | + Expect(err).NotTo(HaveOccurred()) |
| 104 | + replicas := int32(1) |
| 105 | + return workloadv1beta2.AppWrapperComponent{ |
| 106 | + PodSets: []workloadv1beta2.AppWrapperPodSet{{Replicas: &replicas, Path: "template"}}, |
| 107 | + Template: runtime.RawExtension{Raw: jsonBytes}, |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +const serviceYAML = ` |
| 112 | +apiVersion: v1 |
| 113 | +kind: Service |
| 114 | +metadata: |
| 115 | + name: %v |
| 116 | +spec: |
| 117 | + selector: |
| 118 | + app: test |
| 119 | + ports: |
| 120 | + - protocol: TCP |
| 121 | + port: 80 |
| 122 | + targetPort: 8080` |
| 123 | + |
| 124 | +func service() workloadv1beta2.AppWrapperComponent { |
| 125 | + yamlString := fmt.Sprintf(serviceYAML, randName("service")) |
| 126 | + jsonBytes, err := yaml.YAMLToJSON([]byte(yamlString)) |
| 127 | + Expect(err).NotTo(HaveOccurred()) |
| 128 | + return workloadv1beta2.AppWrapperComponent{ |
| 129 | + PodSets: []workloadv1beta2.AppWrapperPodSet{}, |
| 130 | + Template: runtime.RawExtension{Raw: jsonBytes}, |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +const deploymentYAML = ` |
| 135 | +apiVersion: apps/v1 |
| 136 | +kind: Deployment |
| 137 | +metadata: |
| 138 | + name: %v |
| 139 | + labels: |
| 140 | + app: test |
| 141 | +spec: |
| 142 | + replicas: %v |
| 143 | + selector: |
| 144 | + matchLabels: |
| 145 | + app: test |
| 146 | + template: |
| 147 | + metadata: |
| 148 | + labels: |
| 149 | + app: test |
| 150 | + spec: |
| 151 | + terminationGracePeriodSeconds: 0 |
| 152 | + containers: |
| 153 | + - name: busybox |
| 154 | + image: quay.io/project-codeflare/busybox:1.36 |
| 155 | + command: ["sh", "-c", "sleep 10000"] |
| 156 | + resources: |
| 157 | + requests: |
| 158 | + cpu: %v` |
| 159 | + |
| 160 | +func deployment(replicaCount int, milliCPU int64) workloadv1beta2.AppWrapperComponent { |
| 161 | + yamlString := fmt.Sprintf(deploymentYAML, |
| 162 | + randName("deployment"), |
| 163 | + replicaCount, |
| 164 | + resource.NewMilliQuantity(milliCPU, resource.DecimalSI)) |
| 165 | + |
| 166 | + jsonBytes, err := yaml.YAMLToJSON([]byte(yamlString)) |
| 167 | + Expect(err).NotTo(HaveOccurred()) |
| 168 | + replicas := int32(replicaCount) |
| 169 | + return workloadv1beta2.AppWrapperComponent{ |
| 170 | + PodSets: []workloadv1beta2.AppWrapperPodSet{{Replicas: &replicas, Path: "template.spec.template"}}, |
| 171 | + Template: runtime.RawExtension{Raw: jsonBytes}, |
| 172 | + } |
| 173 | +} |
0 commit comments