Skip to content

Commit e2f332d

Browse files
committed
Initial draft for client-go abstraction.
1 parent 9dc8764 commit e2f332d

File tree

10 files changed

+815
-0
lines changed

10 files changed

+815
-0
lines changed

examples/deployment/main.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/opensearch-project/opensearch-go v1.1.0
88
github.com/prometheus/client_golang v1.15.1
99
github.com/prometheus/common v0.44.0
10+
github.com/sirupsen/logrus v1.9.3
1011
k8s.io/api v0.27.1
1112
k8s.io/apimachinery v0.27.1
1213
k8s.io/client-go v0.27.1
@@ -16,7 +17,9 @@ require (
1617
require (
1718
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
1819
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
20+
github.com/imdario/mergo v0.3.6 // indirect
1921
github.com/pmezard/go-difflib v1.0.0 // indirect
22+
github.com/spf13/pflag v1.0.5 // indirect
2023
github.com/stretchr/objx v0.5.0 // indirect
2124
)
2225

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLe
6464
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
6565
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
6666
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
67+
github.com/imdario/mergo v0.3.6 h1:xTNEAn+kxVO7dTZGu0CegyqKZmoWFI0rF8UxjlB2d28=
68+
github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
6769
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
6870
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
6971
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
@@ -110,7 +112,10 @@ github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdO
110112
github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY=
111113
github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI=
112114
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
115+
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
116+
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
113117
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
118+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
114119
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
115120
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
116121
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
@@ -164,6 +169,7 @@ golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7w
164169
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
165170
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
166171
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
172+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
167173
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
168174
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
169175
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=

k8s/deamonset.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package k8s
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"k8s.io/client-go/kubernetes"
8+
)
9+
10+
// Common params shared across the class
11+
type DeamonSetResource struct {
12+
clientSet *kubernetes.Clientset
13+
// DeamonSet resource attributes and metadata
14+
}
15+
16+
// DeamonSet specific params
17+
type DeamonSetParams struct {
18+
}
19+
20+
func (p *DeamonSetResource) Create(ctx context.Context, DeamonSetParams interface{}, dryRun bool) (interface{}, error) {
21+
fmt.Println("Create DeamonSet here")
22+
return nil, nil
23+
}
24+
25+
func (p *DeamonSetResource) Update(ctx context.Context, DeamonSetParams interface{}, dryRun bool) (interface{}, error) {
26+
fmt.Println("Updating DeamonSet here")
27+
return nil, nil
28+
}
29+
30+
func (p *DeamonSetResource) Get(ctx context.Context, DeamonSetParams interface{}) (interface{}, error) {
31+
fmt.Println("Getting DeamonSet here")
32+
return nil, nil
33+
}
34+
35+
func (p *DeamonSetResource) Delete(ctx context.Context, DeamonSetParams interface{}) error {
36+
fmt.Println("Deleting DeamonSet here")
37+
return nil
38+
}

0 commit comments

Comments
 (0)