|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/cloud-bulldozer/go-commons/k8s" |
| 8 | + log "github.com/sirupsen/logrus" |
| 9 | + appsv1 "k8s.io/api/apps/v1" |
| 10 | + v1 "k8s.io/api/core/v1" |
| 11 | +) |
| 12 | + |
| 13 | +func main() { |
| 14 | + // Usage on common Kubernetes Repository |
| 15 | + kubeRepo, err := k8s.NewKubernetesRepository() |
| 16 | + if err != nil { |
| 17 | + log.Info("Some error occured") |
| 18 | + } |
| 19 | + log.Infof("kube Repo object : %+v", kubeRepo) |
| 20 | + |
| 21 | + var res k8s.Resource = &kubeRepo.Deployment |
| 22 | + if _, ok := res.(*k8s.DeploymentResource); ok { |
| 23 | + fmt.Printf("DeploymentResource implements Resource interface. %+v", res) |
| 24 | + } else { |
| 25 | + fmt.Printf("DeploymentResource does not implement Resource interface. %+v", res) |
| 26 | + } |
| 27 | + deploymentParams := k8s.DeploymentParams{ |
| 28 | + Name: "testdeployment", |
| 29 | + Namespace: "default", |
| 30 | + Replicas: 2, |
| 31 | + SelectorLabels: "app=test", |
| 32 | + MetadataLabels: "app=test", |
| 33 | + NodeSelectorLabels: "app=test", |
| 34 | + Containers: []v1.Container{ |
| 35 | + { |
| 36 | + Name: "sleep", |
| 37 | + Image: "gcr.io/google_containers/pause-amd64:3.0", |
| 38 | + ImagePullPolicy: v1.PullAlways, |
| 39 | + }, |
| 40 | + }, |
| 41 | + } |
| 42 | + deployParams, err := kubeRepo.Deployment.Create(context.Background(), deploymentParams, false) |
| 43 | + if err != nil { |
| 44 | + fmt.Println("Error creating Deployment:", err) |
| 45 | + } |
| 46 | + log.Infof("Created Deployment: %+v", deployParams.(k8s.DeploymentParams).Name) |
| 47 | + |
| 48 | + deploymentParams = k8s.DeploymentParams{ |
| 49 | + Name: "testdeployment", |
| 50 | + Namespace: "default", |
| 51 | + SelectorLabels: "app=test", |
| 52 | + MetadataLabels: "app=test", |
| 53 | + Replicas: 4, |
| 54 | + Containers: []v1.Container{ |
| 55 | + { |
| 56 | + Name: "sleep", |
| 57 | + Image: "gcr.io/google_containers/pause-amd64:3.0", |
| 58 | + ImagePullPolicy: v1.PullAlways, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | + deployParams, err = kubeRepo.Deployment.Update(context.Background(), deploymentParams, false) |
| 63 | + if err != nil { |
| 64 | + fmt.Println("Error Updating Deployment:", err) |
| 65 | + } |
| 66 | + log.Infof("Updated Deployment: %+v", deployParams.(k8s.DeploymentParams).Name) |
| 67 | + |
| 68 | + deploymentParams = k8s.DeploymentParams{ |
| 69 | + Deployment: deployParams.(k8s.DeploymentParams).Deployment, |
| 70 | + } |
| 71 | + |
| 72 | + deployment, error := kubeRepo.Deployment.Get(context.Background(), deploymentParams) |
| 73 | + if error != nil { |
| 74 | + fmt.Println("Error Getting Deployment:", error) |
| 75 | + } |
| 76 | + log.Infof("Got Deployment: %+v", deployment.(*appsv1.Deployment)) |
| 77 | + |
| 78 | + deploymentParams = k8s.DeploymentParams{ |
| 79 | + Name: "testdeployment", |
| 80 | + Namespace: "default", |
| 81 | + } |
| 82 | + err = kubeRepo.Deployment.Delete(context.Background(), deploymentParams) |
| 83 | + if err != nil { |
| 84 | + fmt.Println("Error Deleting Deployment:", err) |
| 85 | + } |
| 86 | + log.Infof("Deleted Deployment: %+v", deploymentParams.Name) |
| 87 | + |
| 88 | + // Usage of individual component explicitly |
| 89 | + deploymentResource := &k8s.DeploymentResource{} |
| 90 | + deployment_dup := k8s.DeploymentParams{ |
| 91 | + Name: "testdeployment", |
| 92 | + Namespace: "default", |
| 93 | + Replicas: 2, |
| 94 | + SelectorLabels: "app=test", |
| 95 | + MetadataLabels: "app=test", |
| 96 | + NodeSelectorLabels: "app=test", |
| 97 | + Containers: []v1.Container{ |
| 98 | + { |
| 99 | + Name: "sleep", |
| 100 | + Image: "gcr.io/google_containers/pause-amd64:3.0", |
| 101 | + ImagePullPolicy: v1.PullAlways, |
| 102 | + }, |
| 103 | + }, |
| 104 | + } |
| 105 | + deployParams, err = deploymentResource.Create(context.Background(), deployment_dup, false) |
| 106 | + if err != nil { |
| 107 | + fmt.Println("Error creating Deployment:", err) |
| 108 | + } |
| 109 | + log.Infof("Created Deployment: %+v", deployParams.(k8s.DeploymentParams).Name) |
| 110 | + |
| 111 | + deploymentParams = k8s.DeploymentParams{ |
| 112 | + Name: "testdeployment", |
| 113 | + Namespace: "default", |
| 114 | + } |
| 115 | + err = deploymentResource.Delete(context.Background(), deploymentParams) |
| 116 | + if err != nil { |
| 117 | + fmt.Println("Error Deleting Deployment:", err) |
| 118 | + } |
| 119 | + log.Infof("Deleted Deployment: %+v", deploymentParams.Name) |
| 120 | +} |
0 commit comments