Created
May 6, 2019 22:03
-
-
Save ian-howell/95d2676a2d802c79462a2ccb99fa2f75 to your computer and use it in GitHub Desktop.
k8sobjects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| yaml "github.com/ghodss/yaml" | |
| v1 "k8s.io/api/core/v1" | |
| v1beta1 "k8s.io/api/extensions/v1beta1" | |
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
| ) | |
| type deployment struct { | |
| Name string | |
| PodSpec v1.PodSpec | |
| Replicas *int32 | |
| } | |
| type pod struct { | |
| Name string | |
| Containers []container | |
| } | |
| type container struct { | |
| Image string | |
| Name string | |
| Command []string | |
| Args []string | |
| } | |
| func main() { | |
| mycontainer := container{ | |
| Name: "alpine", | |
| Image: "alpine:3.7", | |
| Command: []string{"/bin/sleep", "9000"}, | |
| } | |
| mypod := pod{Name: "alpine"} | |
| mypod.add(mycontainer) | |
| replicas := int32(3) | |
| mydeployment := deployment{ | |
| Name: "alpine-deployment", | |
| Replicas: &replicas, | |
| } | |
| mydeployment.add(mypod) | |
| deploymentJSON, err := yaml.Marshal(createDeployment(mydeployment)) | |
| if err != nil { | |
| fmt.Println(err) | |
| } else { | |
| fmt.Println(string(deploymentJSON)) | |
| } | |
| } | |
| func createContainer(custom container) v1.Container { | |
| return v1.Container{ | |
| Name: custom.Name, | |
| Image: custom.Image, | |
| Command: custom.Command, | |
| Args: custom.Args, | |
| } | |
| } | |
| func createPod(custom pod) v1.Pod { | |
| returnPod := v1.Pod{ | |
| ObjectMeta: metav1.ObjectMeta{ | |
| Name: custom.Name, | |
| CreationTimestamp: metav1.Now(), | |
| }, | |
| } | |
| for _, container := range custom.Containers { | |
| returnPod.Spec.Containers = append(returnPod.Spec.Containers, createContainer(container)) | |
| } | |
| return returnPod | |
| } | |
| func (p *pod) add(container container) { | |
| p.Containers = append(p.Containers, container) | |
| } | |
| func createDeployment(custom deployment) v1beta1.Deployment { | |
| returnDeployment := v1beta1.Deployment{ | |
| ObjectMeta: metav1.ObjectMeta{ | |
| Name: custom.Name, | |
| }, | |
| Spec: v1beta1.DeploymentSpec{ | |
| Replicas: custom.Replicas, | |
| Template: v1.PodTemplateSpec{ | |
| Spec: custom.PodSpec, | |
| }, | |
| }, | |
| } | |
| return returnDeployment | |
| } | |
| func (d *deployment) add(pod pod) { | |
| d.PodSpec = createPod(pod).Spec | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| metadata: | |
| creationTimestamp: null | |
| name: alpine-deployment | |
| spec: | |
| replicas: 3 | |
| strategy: {} | |
| template: | |
| metadata: | |
| creationTimestamp: null | |
| spec: | |
| containers: | |
| - command: | |
| - /bin/sleep | |
| - "9000" | |
| image: alpine:3.7 | |
| name: alpine | |
| resources: {} | |
| status: {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment