Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment 3 & 4 #68

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions assign1/client-go/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package clientgo

import (
"assig1/models"
"fmt"

appsv1 "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
appV1 "k8s.io/client-go/kubernetes/typed/apps/v1"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
)

// ClientGo ...
type ClientGo interface {
CreatePod() (*core.Pod, error)
UpdatePod(podName string) error
GetPods(podName string) (*core.PodList, error)
DeletePod(podName string) error

CreateDeployment() (*appsv1.Deployment, error)
UpdateDeployment(deploymentName string) error
GetDeployments() (*appsv1.DeploymentList, error)
DeleteDeployment(deploymentName string) error

CreateService() (*corev1.Service, error)
UpdateService(serviceName string) error
GetServices() (*corev1.ServiceList, error)
DeleteService(podName string) error
}

// ClientGoClient ...
type ClientGoClient struct {
podClient v1.PodInterface
serviceClient v1.ServiceInterface
deploymentClient appV1.DeploymentInterface
setUpEssentials *models.ClientGoEssentials
}

// NewClientGoClient ...
func NewClientGoClient(setUpEssentials *models.ClientGoEssentials) (*ClientGoClient, error) {
config, err := rest.InClusterConfig()
if err != nil {
fmt.Printf("Error creating in-cluster config: %v\n", err)
return nil, err
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
fmt.Printf("Error creating clientset: %v\n", err)
return nil, err
}

podClient := clientset.CoreV1().Pods(setUpEssentials.Namespace)
serviceClient := clientset.CoreV1().Services(setUpEssentials.Namespace)
deploymentClient := clientset.AppsV1().Deployments(setUpEssentials.Namespace)
return &ClientGoClient{
setUpEssentials: setUpEssentials,
podClient: podClient,
serviceClient: serviceClient,
deploymentClient: deploymentClient,
}, nil
}
90 changes: 90 additions & 0 deletions assign1/client-go/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package clientgo

import (
"assig1/constants"
"context"
"fmt"

appsv1 "k8s.io/api/apps/v1"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// CreateDeployment ...
func (cg *ClientGoClient) CreateDeployment() (*appsv1.Deployment, error) {
replicas := int32(2)
newDeployment := &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1",
Kind: "Deployment",
},
ObjectMeta: metav1.ObjectMeta{
Name: cg.setUpEssentials.DeploymentName,
Namespace: cg.setUpEssentials.Namespace,
Labels: map[string]string{
"app": "ariarijitAssign1",
},
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "arijitAssign1",
},
},
Replicas: &replicas,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "arijitAssign1",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "nginx",
Image: constants.NginxImage,
},
},
},
},
},
}

createdDeployment, err := cg.deploymentClient.Create(context.Background(), newDeployment, metav1.CreateOptions{})
if err != nil {
return nil, err
}

return createdDeployment, nil
}

// UpdateDeployment ...
func (cg *ClientGoClient) UpdateDeployment(deploymentName string) error {
updatedDeployment, err := cg.deploymentClient.Get(context.Background(), deploymentName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("error getting Pod for update: %v", err)
}

updatedDeployment.SetGenerateName("generated-updated-deployment")

_, err = cg.deploymentClient.Update(context.Background(), updatedDeployment, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("error updating Pod: %v", err)
}

return nil
}

// GetDeployments ...
func (cg *ClientGoClient) GetDeployments() (*appsv1.DeploymentList, error) {
return cg.deploymentClient.List(context.Background(), metav1.ListOptions{})
}

// DeleteDeployment ...
func (cg *ClientGoClient) DeleteDeployment(deploymentName string) error {
deletePolicy := metav1.DeletePropagationForeground
return cg.deploymentClient.Delete(context.Background(), deploymentName, metav1.DeleteOptions{
PropagationPolicy: &deletePolicy,
})
}
64 changes: 64 additions & 0 deletions assign1/client-go/pod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package clientgo

import (
"assig1/constants"
"context"
"fmt"

core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// CreatePod ...
func (cg *ClientGoClient) CreatePod() (*core.Pod, error) {
newPod := &core.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: cg.setUpEssentials.PodName,
Namespace: cg.setUpEssentials.Namespace,
},
Spec: core.PodSpec{
Containers: []core.Container{
{
Name: "nginx",
Image: constants.NginxImage,
ImagePullPolicy: core.PullIfNotPresent,
},
},
},
}

createdPod, err := cg.podClient.Create(context.Background(), newPod, metav1.CreateOptions{})
if err != nil {
return nil, err
}

return createdPod, nil
}

// UpdatePod ...
func (cg *ClientGoClient) UpdatePod(podName string) error {
updatedPod, err := cg.podClient.Get(context.Background(), podName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("error getting Pod for update: %v", err)
}

updatedPod.SetGenerateName("generated-updated-name")
updatedPod.Spec.Containers[0].Image = "nginx:1.21"

_, err = cg.podClient.Update(context.Background(), updatedPod, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("error updating Pod: %v", err)
}

return nil
}

// GetPods ...
func (cg *ClientGoClient) GetPods() (*core.PodList, error) {
return cg.podClient.List(context.Background(), metav1.ListOptions{})
}

// DeletePod ...
func (cg *ClientGoClient) DeletePod(podName string) error {
return cg.podClient.Delete(context.Background(), podName, metav1.DeleteOptions{})
}
73 changes: 73 additions & 0 deletions assign1/client-go/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package clientgo

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

// CreateService ...
func (cg *ClientGoClient) CreateService() (*corev1.Service, error) {
newService := &corev1.Service{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Service",
},
ObjectMeta: metav1.ObjectMeta{
Name: cg.setUpEssentials.ServiceName,
Namespace: cg.setUpEssentials.Namespace,
},
Spec: corev1.ServiceSpec{
Selector: map[string]string{
"app": "arijitAssign1",
},
Ports: []corev1.ServicePort{
{
Protocol: corev1.ProtocolTCP,
Port: 8081,
TargetPort: intstr.FromInt(8081),
},
},
},
}

createdService, err := cg.serviceClient.Create(context.Background(), newService, metav1.CreateOptions{})
if err != nil {
return nil, err
}

return createdService, nil
}

// UpdateService ...
func (cg *ClientGoClient) UpdateService(serviceName string) error {
updatedService, err := cg.serviceClient.Get(context.Background(), serviceName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("error getting Pod for update: %v", err)
}

updatedService.SetGenerateName("generated-updated-service")

_, err = cg.serviceClient.Update(context.Background(), updatedService, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("error updating Pod: %v", err)
}

return nil
}

// GetServices ...
func (cg *ClientGoClient) GetServices() (*corev1.ServiceList, error) {
return cg.serviceClient.List(context.Background(), metav1.ListOptions{})
}

// DeleteService ...
func (cg *ClientGoClient) DeleteService(podName string) error {
deletePolicy := metav1.DeletePropagationForeground
return cg.serviceClient.Delete(context.Background(), podName, metav1.DeleteOptions{
PropagationPolicy: &deletePolicy,
})
}
5 changes: 5 additions & 0 deletions assign1/constants/clientog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package constants

const (
NginxImage string = "nginx:latest"
)
41 changes: 41 additions & 0 deletions assign1/controller-runtime/crt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package crt

import (
"fmt"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

// CRT ...
type CRT interface {
CreatePod(podName string) error
UpdatePod(podName string) error
DeletePod(podName string) error
GetPod(podName string) (*appsv1.Deployment, error)

CreateService(serviceName string) error
UpdateService(serviceName string) error
DeleteService(serviceName string) error
GetService(serviceName string) (*corev1.Service, error)
}

// CRTClient ...
type CRTClient struct {
apiServerClient client.Client
namespace string
}

// NewCRTClient ...
func NewCRTClient(namespace string) (*CRTClient, error) {
apiServerClient, err := client.New(config.GetConfigOrDie(), client.Options{})
if err != nil {
return nil, fmt.Errorf("failed to initialize the crt client : %v", err)
}
return &CRTClient{
apiServerClient: apiServerClient,
namespace: namespace,
}, nil
}
1 change: 1 addition & 0 deletions assign1/controller-runtime/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package crt
Loading