-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial draft for client-go abstraction.
- Loading branch information
1 parent
9dc8764
commit 9ee69ca
Showing
10 changed files
with
816 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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) | ||
} |
This file contains 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
This file contains 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
This file contains 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
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 | ||
} |
Oops, something went wrong.