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

chore: unittest for k8s pkg #416

Merged
merged 15 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore: rename cli to client
  • Loading branch information
mojtaba-esk committed Jun 13, 2024
commit cb407e5d0cf6b1880ec69467cf4c5cf32b171465
38 changes: 19 additions & 19 deletions pkg/instance/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (i *Instance) deployService(ctx context.Context, portsTCP, portsUDP []int)
labels := i.getLabels()
labelSelectors := labels

service, err := i.K8sCli.CreateService(ctx, serviceName, labels, labelSelectors, portsTCP, portsUDP)
service, err := i.K8sClient.CreateService(ctx, serviceName, labels, labelSelectors, portsTCP, portsUDP)
if err != nil {
return ErrDeployingService.WithParams(i.k8sName).Wrap(err)
}
Expand All @@ -111,7 +111,7 @@ func (i *Instance) patchService(ctx context.Context, portsTCP, portsUDP []int) e
labels := i.getLabels()
labelSelectors := labels

service, err := i.K8sCli.PatchService(ctx, serviceName, labels, labelSelectors, portsTCP, portsUDP)
service, err := i.K8sClient.PatchService(ctx, serviceName, labels, labelSelectors, portsTCP, portsUDP)
if err != nil {
return ErrPatchingService.WithParams(serviceName).Wrap(err)
}
Expand All @@ -122,7 +122,7 @@ func (i *Instance) patchService(ctx context.Context, portsTCP, portsUDP []int) e

// destroyService destroys the service for the instance
func (i *Instance) destroyService(ctx context.Context) error {
return i.K8sCli.DeleteService(ctx, i.k8sName)
return i.K8sClient.DeleteService(ctx, i.k8sName)
}

// deployPod deploys the pod for the instance
Expand All @@ -131,24 +131,24 @@ func (i *Instance) deployPod(ctx context.Context) error {
labels := i.getLabels()

// create a service account for the pod
if err := i.K8sCli.CreateServiceAccount(ctx, i.k8sName, labels); err != nil {
if err := i.K8sClient.CreateServiceAccount(ctx, i.k8sName, labels); err != nil {
return ErrFailedToCreateServiceAccount.Wrap(err)
}

// create a role and role binding for the pod if there are policy rules
if len(i.policyRules) > 0 {
if err := i.K8sCli.CreateRole(ctx, i.k8sName, labels, i.policyRules); err != nil {
if err := i.K8sClient.CreateRole(ctx, i.k8sName, labels, i.policyRules); err != nil {
return ErrFailedToCreateRole.Wrap(err)
}
if err := i.K8sCli.CreateRoleBinding(ctx, i.k8sName, labels, i.k8sName, i.k8sName); err != nil {
if err := i.K8sClient.CreateRoleBinding(ctx, i.k8sName, labels, i.k8sName, i.k8sName); err != nil {
return ErrFailedToCreateRoleBinding.Wrap(err)
}
}

replicaSetSetConfig := i.prepareReplicaSetConfig()

// Deploy the statefulSet
replicaSet, err := i.K8sCli.CreateReplicaSet(ctx, replicaSetSetConfig, true)
replicaSet, err := i.K8sClient.CreateReplicaSet(ctx, replicaSetSetConfig, true)
if err != nil {
return ErrFailedToDeployPod.Wrap(err)
}
Expand All @@ -167,21 +167,21 @@ func (i *Instance) deployPod(ctx context.Context) error {
// Skips if the pod is already destroyed
func (i *Instance) destroyPod(ctx context.Context) error {
grace := int64(0)
err := i.K8sCli.DeleteReplicaSetWithGracePeriod(ctx, i.k8sName, &grace)
err := i.K8sClient.DeleteReplicaSetWithGracePeriod(ctx, i.k8sName, &grace)
if err != nil {
return ErrFailedToDeletePod.Wrap(err)
}

// Delete the service account for the pod
if err := i.K8sCli.DeleteServiceAccount(ctx, i.k8sName); err != nil {
if err := i.K8sClient.DeleteServiceAccount(ctx, i.k8sName); err != nil {
return ErrFailedToDeleteServiceAccount.Wrap(err)
}
// Delete the role and role binding for the pod if there are policy rules
if len(i.policyRules) > 0 {
if err := i.K8sCli.DeleteRole(ctx, i.k8sName); err != nil {
if err := i.K8sClient.DeleteRole(ctx, i.k8sName); err != nil {
return ErrFailedToDeleteRole.Wrap(err)
}
if err := i.K8sCli.DeleteRoleBinding(ctx, i.k8sName); err != nil {
if err := i.K8sClient.DeleteRoleBinding(ctx, i.k8sName); err != nil {
return ErrFailedToDeleteRoleBinding.Wrap(err)
}
}
Expand All @@ -193,7 +193,7 @@ func (i *Instance) destroyPod(ctx context.Context) error {
func (i *Instance) deployOrPatchService(ctx context.Context, portsTCP, portsUDP []int) error {
if len(portsTCP) != 0 || len(portsUDP) != 0 {
logrus.Debugf("Ports not empty, deploying service for instance '%s'", i.k8sName)
svc, _ := i.K8sCli.GetService(ctx, i.k8sName)
svc, _ := i.K8sClient.GetService(ctx, i.k8sName)
if svc == nil {
err := i.deployService(ctx, portsTCP, portsUDP)
if err != nil {
Expand All @@ -215,15 +215,15 @@ func (i *Instance) deployVolume(ctx context.Context) error {
for _, volume := range i.volumes {
size.Add(resource.MustParse(volume.Size))
}
i.K8sCli.CreatePersistentVolumeClaim(ctx, i.k8sName, i.getLabels(), size)
i.K8sClient.CreatePersistentVolumeClaim(ctx, i.k8sName, i.getLabels(), size)
logrus.Debugf("Deployed persistent volume '%s'", i.k8sName)

return nil
}

// destroyVolume destroys the volume for the instance
func (i *Instance) destroyVolume(ctx context.Context) error {
i.K8sCli.DeletePersistentVolumeClaim(ctx, i.k8sName)
i.K8sClient.DeletePersistentVolumeClaim(ctx, i.k8sName)
logrus.Debugf("Destroyed persistent volume '%s'", i.k8sName)

return nil
Expand Down Expand Up @@ -256,7 +256,7 @@ func (i *Instance) deployFiles(ctx context.Context) error {
}

// create configmap
if _, err := i.K8sCli.CreateConfigMap(ctx, i.k8sName, i.getLabels(), data); err != nil {
if _, err := i.K8sClient.CreateConfigMap(ctx, i.k8sName, i.getLabels(), data); err != nil {
return ErrFailedToCreateConfigMap.Wrap(err)
}

Expand All @@ -267,7 +267,7 @@ func (i *Instance) deployFiles(ctx context.Context) error {

// destroyFiles destroys the files for the instance
func (i *Instance) destroyFiles(ctx context.Context) error {
if err := i.K8sCli.DeleteConfigMap(ctx, i.k8sName); err != nil {
if err := i.K8sClient.DeleteConfigMap(ctx, i.k8sName); err != nil {
return ErrFailedToDeleteConfigMap.Wrap(err)
}

Expand Down Expand Up @@ -508,7 +508,7 @@ func (i *Instance) prepareReplicaSetConfig() k8s.ReplicaSetConfig {
}
// Generate the pod configuration
podConfig := k8s.PodConfig{
Namespace: i.K8sCli.Namespace(),
Namespace: i.K8sClient.Namespace(),
Name: i.k8sName,
Labels: i.getLabels(),
ServiceAccountName: i.k8sName,
Expand All @@ -518,7 +518,7 @@ func (i *Instance) prepareReplicaSetConfig() k8s.ReplicaSetConfig {
}
// Generate the ReplicaSet configuration
statefulSetConfig := k8s.ReplicaSetConfig{
Namespace: i.K8sCli.Namespace(),
Namespace: i.K8sClient.Namespace(),
Name: i.k8sName,
Labels: i.getLabels(),
Replicas: 1,
Expand All @@ -535,7 +535,7 @@ func (i *Instance) setImageWithGracePeriod(ctx context.Context, imageName string
replicaSetConfig := i.prepareReplicaSetConfig()

// Replace the pod with a new one, using the given image
_, err := i.K8sCli.ReplaceReplicaSetWithGracePeriod(ctx, replicaSetConfig, gracePeriod)
_, err := i.K8sClient.ReplaceReplicaSetWithGracePeriod(ctx, replicaSetConfig, gracePeriod)
if err != nil {
return ErrReplacingPod.Wrap(err)
}
Expand Down
28 changes: 14 additions & 14 deletions pkg/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,13 @@ func (i *Instance) PortForwardTCP(ctx context.Context, port int) (int, error) {
}

// Forward the port
pod, err := i.K8sCli.GetFirstPodFromReplicaSet(ctx, i.k8sName)
pod, err := i.K8sClient.GetFirstPodFromReplicaSet(ctx, i.k8sName)
if err != nil {
return -1, ErrGettingPodFromReplicaSet.WithParams(i.k8sName).Wrap(err)
}

for attempt := 1; attempt <= maxRetries; attempt++ {
err := i.K8sCli.PortForwardPod(ctx, pod.Name, localPort, port)
err := i.K8sClient.PortForwardPod(ctx, pod.Name, localPort, port)
if err == nil {
break
}
Expand Down Expand Up @@ -385,13 +385,13 @@ func (i *Instance) ExecuteCommand(ctx context.Context, command ...string) (strin
eErr = ErrExecutingCommandInInstance.WithParams(command, i.k8sName)
}

pod, err := i.K8sCli.GetFirstPodFromReplicaSet(ctx, instanceName)
pod, err := i.K8sClient.GetFirstPodFromReplicaSet(ctx, instanceName)
if err != nil {
return "", ErrGettingPodFromReplicaSet.WithParams(i.k8sName).Wrap(err)
}

commandWithShell := []string{"/bin/sh", "-c", strings.Join(command, " ")}
output, err := i.K8sCli.RunCommandInPod(ctx, pod.Name, containerName, commandWithShell)
output, err := i.K8sClient.RunCommandInPod(ctx, pod.Name, containerName, commandWithShell)
if err != nil {
return "", eErr.Wrap(err)
}
Expand Down Expand Up @@ -468,7 +468,7 @@ func (i *Instance) AddFile(src string, dest string, chown string) error {
if os.IsNotExist(err) || srcInfo.IsDir() {
return ErrSrcDoesNotExistOrIsDirectory.WithParams(src).Wrap(err)
}
file := i.K8sCli.NewFile(dstPath, dest)
file := i.K8sClient.NewFile(dstPath, dest)

// the user provided a chown string (e.g. "10001:10001") and we only need the group (second part)
parts := strings.Split(chown, ":")
Expand Down Expand Up @@ -661,7 +661,7 @@ func (i *Instance) AddVolumeWithOwner(path, size string, owner int64) error {
logrus.Debugf("Maximum volumes exceeded for instance '%s', volumes: %d", i.name, len(i.volumes))
return ErrMaximumVolumesExceeded.WithParams(i.name)
}
volume := i.K8sCli.NewVolume(path, size, owner)
volume := i.K8sClient.NewVolume(path, size, owner)
i.volumes = append(i.volumes, volume)
logrus.Debugf("Added volume '%s' with size '%s' and owner '%d' to instance '%s'", path, size, owner, i.name)
return nil
Expand Down Expand Up @@ -716,14 +716,14 @@ func (i *Instance) GetIP(ctx context.Context) (string, error) {
return i.kubernetesService.Spec.ClusterIP, nil
}
// If not, proceed with the existing logic to deploy the service and get the IP
svc, err := i.K8sCli.GetService(ctx, i.k8sName)
svc, err := i.K8sClient.GetService(ctx, i.k8sName)
if err != nil || svc == nil {
// Service does not exist, so we need to deploy it
err := i.deployService(ctx, i.portsTCP, i.portsUDP)
if err != nil {
return "", ErrDeployingServiceForInstance.WithParams(i.k8sName).Wrap(err)
}
svc, err = i.K8sCli.GetService(ctx, i.k8sName)
svc, err = i.K8sClient.GetService(ctx, i.k8sName)
if err != nil {
return "", ErrGettingServiceForInstance.WithParams(i.k8sName).Wrap(err)
}
Expand Down Expand Up @@ -1079,7 +1079,7 @@ func (i *Instance) IsRunning(ctx context.Context) (bool, error) {
return false, ErrCheckingIfInstanceRunningNotAllowed.WithParams(i.state.String())
}

return i.K8sCli.IsReplicaSetRunning(ctx, i.k8sName)
return i.K8sClient.IsReplicaSetRunning(ctx, i.k8sName)
}

// WaitInstanceIsRunning waits until the instance is running
Expand Down Expand Up @@ -1119,7 +1119,7 @@ func (i *Instance) DisableNetwork(ctx context.Context) error {
"knuu.sh/type": ExecutorInstance.String(),
}

err := i.K8sCli.CreateNetworkPolicy(ctx, i.k8sName, i.getLabels(), executorSelectorMap, executorSelectorMap)
err := i.K8sClient.CreateNetworkPolicy(ctx, i.k8sName, i.getLabels(), executorSelectorMap, executorSelectorMap)
if err != nil {
return ErrDisablingNetwork.WithParams(i.k8sName).Wrap(err)
}
Expand Down Expand Up @@ -1234,7 +1234,7 @@ func (i *Instance) EnableNetwork(ctx context.Context) error {
return ErrEnablingNetworkNotAllowed.WithParams(i.state.String())
}

err := i.K8sCli.DeleteNetworkPolicy(ctx, i.k8sName)
err := i.K8sClient.DeleteNetworkPolicy(ctx, i.k8sName)
if err != nil {
return ErrEnablingNetwork.WithParams(i.k8sName).Wrap(err)
}
Expand All @@ -1248,7 +1248,7 @@ func (i *Instance) NetworkIsDisabled(ctx context.Context) (bool, error) {
return false, ErrCheckingIfNetworkDisabledNotAllowed.WithParams(i.state.String())
}

return i.K8sCli.NetworkPolicyExists(ctx, i.k8sName), nil
return i.K8sClient.NetworkPolicyExists(ctx, i.k8sName), nil
}

// WaitInstanceIsStopped waits until the instance is not running anymore
Expand Down Expand Up @@ -1339,12 +1339,12 @@ func (i *Instance) CreateCustomResource(ctx context.Context, gvr *schema.GroupVe
return ErrCustomResourceDefinitionDoesNotExist.WithParams(gvr.Resource)
}

return i.K8sCli.CreateCustomResource(ctx, i.k8sName, gvr, obj)
return i.K8sClient.CreateCustomResource(ctx, i.k8sName, gvr, obj)
}

// CustomResourceDefinitionExists checks if the custom resource definition exists
func (i *Instance) CustomResourceDefinitionExists(ctx context.Context, gvr *schema.GroupVersionResource) (bool, error) {
return i.K8sCli.CustomResourceDefinitionExists(ctx, gvr), nil
return i.K8sClient.CustomResourceDefinitionExists(ctx, gvr), nil
}

func (i *Instance) AddHost(ctx context.Context, port int) (host string, err error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/instance/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func (i *Instance) createProcessors() Processors {
Actions: []Action{
{
Key: "namespace",
Value: i.K8sCli.Namespace(),
Value: i.K8sClient.Namespace(),
Action: "insert",
},
},
Expand Down
8 changes: 4 additions & 4 deletions pkg/knuu/knuu_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ func InitializeWithScope(testScope string) error {
switch builderType {
case "kubernetes":
tmpKnuu.ImageBuilder = &kaniko.Kaniko{
K8s: tmpKnuu.K8sCli,
Minio: tmpKnuu.MinioCli,
K8s: tmpKnuu.K8sClient,
Minio: tmpKnuu.MinioClient,
}
case "docker", "":
tmpKnuu.ImageBuilder = &docker.Docker{
K8sClientset: tmpKnuu.K8sCli.Clientset(),
K8sNamespace: tmpKnuu.K8sCli.Namespace(),
K8sClientset: tmpKnuu.K8sClient.Clientset(),
K8sNamespace: tmpKnuu.K8sClient.Namespace(),
}
default:
return ErrInvalidKnuuBuilder.WithParams(builderType)
Expand Down
8 changes: 4 additions & 4 deletions pkg/knuu/knuu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func TestNew(t *testing.T) {
validateFunc: func(t *testing.T, k *Knuu) {
assert.NotNil(t, k)
assert.NotNil(t, k.Logger)
assert.NotNil(t, k.K8sCli)
assert.NotNil(t, k.MinioCli)
assert.NotNil(t, k.K8sClient)
assert.NotNil(t, k.MinioClient)
assert.NotNil(t, k.ImageBuilder)
assert.Equal(t, defaultTimeout, k.timeout)
},
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestNew(t *testing.T) {
expectError: false,
validateFunc: func(t *testing.T, k *Knuu) {
assert.NotNil(t, k)
assert.NotNil(t, k.K8sCli)
assert.NotNil(t, k.K8sClient)
},
},
{
Expand All @@ -118,7 +118,7 @@ func TestNew(t *testing.T) {
expectError: false,
validateFunc: func(t *testing.T, k *Knuu) {
assert.NotNil(t, k)
assert.NotNil(t, k.MinioCli)
assert.NotNil(t, k.MinioClient)
},
},
{
Expand Down
10 changes: 5 additions & 5 deletions pkg/knuu/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,31 @@ import (
const minioBucketName = "knuu"

func (k *Knuu) initMinio(ctx context.Context) error {
if k.MinioCli == nil {
if k.MinioClient == nil {
return ErrMinioNotInitialized
}

ok, err := k.MinioCli.IsMinioDeployed(ctx)
ok, err := k.MinioClient.IsMinioDeployed(ctx)
if err != nil {
return err
}
if ok {
return nil
}
return k.MinioCli.DeployMinio(ctx)
return k.MinioClient.DeployMinio(ctx)
}

// contentName is a unique string to identify the content in Minio
func (k *Knuu) PushFileToMinio(ctx context.Context, contentName string, reader io.Reader) error {
if err := k.initMinio(ctx); err != nil {
return err
}
return k.MinioCli.PushToMinio(ctx, reader, contentName, minioBucketName)
return k.MinioClient.PushToMinio(ctx, reader, contentName, minioBucketName)
}

func (k *Knuu) GetMinioURL(ctx context.Context, contentName string) (string, error) {
if err := k.initMinio(ctx); err != nil {
return "", err
}
return k.MinioCli.GetMinioURL(ctx, contentName, minioBucketName)
return k.MinioClient.GetMinioURL(ctx, contentName, minioBucketName)
}
8 changes: 4 additions & 4 deletions pkg/preloader/preloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (p *Preloader) EmptyImages(ctx context.Context) error {
func (p *Preloader) preloadImages(ctx context.Context) error {
// delete the daemonset if no images are preloaded
if len(p.Images) == 0 {
return p.K8sCli.DeleteDaemonSet(ctx, p.K8sName)
return p.K8sClient.DeleteDaemonSet(ctx, p.K8sName)
}
var initContainers []v1.Container

Expand Down Expand Up @@ -110,18 +110,18 @@ func (p *Preloader) preloadImages(ctx context.Context) error {
"knuu.sh/test-started": p.StartTime,
}

exists, err := p.K8sCli.DaemonSetExists(ctx, p.K8sName)
exists, err := p.K8sClient.DaemonSetExists(ctx, p.K8sName)
if err != nil {
return err
}

// update the daemonset if it already exists
if exists {
_, err = p.K8sCli.UpdateDaemonSet(ctx, p.K8sName, labels, initContainers, containers)
_, err = p.K8sClient.UpdateDaemonSet(ctx, p.K8sName, labels, initContainers, containers)
return err
}

// create the daemonset if it doesn't exist
_, err = p.K8sCli.CreateDaemonSet(ctx, p.K8sName, labels, initContainers, containers)
_, err = p.K8sClient.CreateDaemonSet(ctx, p.K8sName, labels, initContainers, containers)
return err
}
Loading
Loading