Skip to content

Commit

Permalink
Initial draft for client-go abstraction.
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnuchalla committed Jul 30, 2023
1 parent 9dc8764 commit 9ee69ca
Show file tree
Hide file tree
Showing 10 changed files with 816 additions and 0 deletions.
120 changes: 120 additions & 0 deletions examples/deployment/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package main

import (
"context"
"fmt"

"github.com/cloud-bulldozer/go-commons/k8s"
log "github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
)

func main() {
// Usage on common Kubernetes Repository
kubeRepo, err := k8s.NewKubernetesRepository()
if err != nil {
log.Info("Some error occured")
}
log.Infof("kube Repo object : %+v", kubeRepo)

var res k8s.Resource = &kubeRepo.Deployment
if _, ok := res.(*k8s.DeploymentResource); ok {
fmt.Printf("DeploymentResource implements Resource interface. %+v", res)
} else {
fmt.Printf("DeploymentResource does not implement Resource interface. %+v", res)
}
deploymentParams := k8s.DeploymentParams{
Name: "testdeployment",
Namespace: "default",
Replicas: 2,
SelectorLabels: "app=test",
MetadataLabels: "app=test",
NodeSelectorLabels: "app=test",
Containers: []v1.Container{
{
Name: "sleep",
Image: "gcr.io/google_containers/pause-amd64:3.0",
ImagePullPolicy: v1.PullAlways,
},
},
}
deployParams, err := kubeRepo.Deployment.Create(context.Background(), deploymentParams, false)
if err != nil {
fmt.Println("Error creating Deployment:", err)
}
log.Infof("Created Deployment: %+v", deployParams.(k8s.DeploymentParams).Name)

deploymentParams = k8s.DeploymentParams{
Name: "testdeployment",
Namespace: "default",
SelectorLabels: "app=test",
MetadataLabels: "app=test",
Replicas: 4,
Containers: []v1.Container{
{
Name: "sleep",
Image: "gcr.io/google_containers/pause-amd64:3.0",
ImagePullPolicy: v1.PullAlways,
},
},
}
deployParams, err = kubeRepo.Deployment.Update(context.Background(), deploymentParams, false)
if err != nil {
fmt.Println("Error Updating Deployment:", err)
}
log.Infof("Updated Deployment: %+v", deployParams.(k8s.DeploymentParams).Name)

deploymentParams = k8s.DeploymentParams{
Deployment: deployParams.(k8s.DeploymentParams).Deployment,
}

deployment, error := kubeRepo.Deployment.Get(context.Background(), deploymentParams)
if error != nil {
fmt.Println("Error Getting Deployment:", error)
}
log.Infof("Got Deployment: %+v", deployment.(*appsv1.Deployment))

deploymentParams = k8s.DeploymentParams{
Name: "testdeployment",
Namespace: "default",
}
err = kubeRepo.Deployment.Delete(context.Background(), deploymentParams)
if err != nil {
fmt.Println("Error Deleting Deployment:", err)
}
log.Infof("Deleted Deployment: %+v", deploymentParams.Name)

// Usage of individual component explicitly
deploymentResource := &k8s.DeploymentResource{}
deployment_dup := k8s.DeploymentParams{
Name: "testdeployment",
Namespace: "default",
Replicas: 2,
SelectorLabels: "app=test",
MetadataLabels: "app=test",
NodeSelectorLabels: "app=test",
Containers: []v1.Container{
{
Name: "sleep",
Image: "gcr.io/google_containers/pause-amd64:3.0",
ImagePullPolicy: v1.PullAlways,
},
},
}
deployParams, err = deploymentResource.Create(context.Background(), deployment_dup, false)
if err != nil {
fmt.Println("Error creating Deployment:", err)
}
log.Infof("Created Deployment: %+v", deployParams.(k8s.DeploymentParams).Name)

deploymentParams = k8s.DeploymentParams{
Name: "testdeployment",
Namespace: "default",
}
err = deploymentResource.Delete(context.Background(), deploymentParams)
if err != nil {
fmt.Println("Error Deleting Deployment:", err)
}
log.Infof("Deleted Deployment: %+v", deploymentParams.Name)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/opensearch-project/opensearch-go v1.1.0
github.com/prometheus/client_golang v1.15.1
github.com/prometheus/common v0.44.0
github.com/sirupsen/logrus v1.9.3
k8s.io/api v0.27.1
k8s.io/apimachinery v0.27.1
k8s.io/client-go v0.27.1
Expand All @@ -16,7 +17,9 @@ require (
require (
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
)

Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLe
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.6 h1:xTNEAn+kxVO7dTZGu0CegyqKZmoWFI0rF8UxjlB2d28=
github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
Expand Down Expand Up @@ -110,7 +112,10 @@ github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdO
github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY=
github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
Expand Down Expand Up @@ -164,6 +169,7 @@ golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
38 changes: 38 additions & 0 deletions k8s/deamonset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package k8s

import (
"context"
"fmt"

"k8s.io/client-go/kubernetes"
)

// Common params shared across the class
type DeamonSetResource struct {
clientSet *kubernetes.Clientset
// DeamonSet resource attributes and metadata
}

// DeamonSet specific params
type DeamonSetParams struct {
}

func (p *DeamonSetResource) Create(ctx context.Context, DeamonSetParams interface{}, dryRun bool) (interface{}, error) {
fmt.Println("Create DeamonSet here")
return nil, nil
}

func (p *DeamonSetResource) Update(ctx context.Context, DeamonSetParams interface{}, dryRun bool) (interface{}, error) {
fmt.Println("Updating DeamonSet here")
return nil, nil
}

func (p *DeamonSetResource) Get(ctx context.Context, DeamonSetParams interface{}) (interface{}, error) {
fmt.Println("Getting DeamonSet here")
return nil, nil
}

func (p *DeamonSetResource) Delete(ctx context.Context, DeamonSetParams interface{}) error {
fmt.Println("Deleting DeamonSet here")
return nil
}
Loading

0 comments on commit 9ee69ca

Please sign in to comment.