Skip to content

Commit

Permalink
Updat the UpdateDeploymentWithTLS
Browse files Browse the repository at this point in the history
Tests are in progress.

Signed-off-by: Veronika Fisarova <[email protected]>
  • Loading branch information
Deydra71 committed Oct 10, 2023
1 parent 4307424 commit 781b3cc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 77 deletions.
14 changes: 8 additions & 6 deletions modules/common/tls/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,27 @@ func (t *TLS) CreateVolumes() []corev1.Volume {
return volumes
}

// AddTLSToDeployment adds the necessary volumes and volume mounts to support TLS in a deployment.
func (t *TLS) AddTLSToDeployment(d *deployment.Deployment) {
// UpdateDeploymentWithTLS updates a given deployment with the necessary volumes and volume mounts to support TLS configurations.
func (t *TLS) UpdateDeploymentWithTLS(ctx context.Context, d *deployment.Deployment, helper HelperInterface) error {
// Debug
fmt.Println("Service SecretName:", t.Service.SecretName)
fmt.Println("CA SecretName:", t.Ca.CaSecretName)

tlsVolumes := t.CreateVolumes()
fmt.Println("Generated TLS Volumes:", tlsVolumes) //Debug
fmt.Println("Generated TLS Volumes:", tlsVolumes) // Debug

tlsVolumeMounts := t.CreateVolumeMounts()
fmt.Println("Generated TLS VolumeMounts:", tlsVolumeMounts) //Debug
fmt.Println("Generated TLS VolumeMounts:", tlsVolumeMounts) // Debug

currentDeployment := d.GetDeployment()

currentDeployment.Spec.Template.Spec.Volumes = append(currentDeployment.Spec.Template.Spec.Volumes, tlsVolumes...)
for idx := range currentDeployment.Spec.Template.Spec.Containers {
currentDeployment.Spec.Template.Spec.Containers[idx].VolumeMounts = append(currentDeployment.Spec.Template.Spec.Containers[idx].VolumeMounts, tlsVolumeMounts...)
}

// Use the update method to make changes
d.UpdateInternalDeployment(currentDeployment)

// TBD: additional logic
_, err := d.CreateOrPatch(ctx, h)
return err
}
116 changes: 45 additions & 71 deletions modules/common/tls/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,39 @@ limitations under the License.
package tls

import (
"fmt"
"context"
"testing"

"github.com/openstack-k8s-operators/lib-common/modules/common/deployment"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type mockDeployment struct {
*deployment.Deployment
}

func (m *mockDeployment) GetDeployment() appsv1.Deployment {
return m.Deployment.GetDeployment()
}

func (m *mockDeployment) UpdateInternalDeployment(deployment appsv1.Deployment) {
m.Deployment = &deployment
}

func (m *mockDeployment) CreateOrPatch(ctx context.Context, h *helper.Helper) (ctrl.Result, error) {
return ctrl.Result{}, nil
}

type mockHelper struct {
*helper.Helper
}

func (m *mockHelper) GetClient() client.Client {
return m.Helper.GetClient()
}
func TestCreateVolumeMounts(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -113,79 +138,28 @@ func TestCreateVolumes(t *testing.T) {
}
}

func TestAddTLSToDeployment(t *testing.T) {
tests := []struct {
name string
service *Service
ca *Ca
wantMountsLen int
wantVolLen int
}{
{
name: "No Secrets",
service: &Service{},
ca: &Ca{},
wantMountsLen: 0,
wantVolLen: 0,
},
{
name: "Only TLS Secret",
service: &Service{SecretName: "test-tls-secret"},
ca: &Ca{},
wantMountsLen: 1,
wantVolLen: 1,
},
{
name: "Only CA Secret",
service: &Service{},
ca: &Ca{CaSecretName: "test-ca1"},
wantMountsLen: 1,
wantVolLen: 1,
},
{
name: "TLS and CA Secrets",
service: &Service{SecretName: "test-tls-secret"},
ca: &Ca{CaSecretName: "test-ca1"},
wantMountsLen: 2,
wantVolLen: 2,
},
func TestUpdateDeploymentWithTLS(t *testing.T) {
// Mock objects
d := &mockDeployment{
Deployment: &deployment.Deployment{},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a dummy deployment object
dummyDeployment := &appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{},
},
},
},
},
}
d := deployment.NewDeployment(dummyDeployment, 0)

tlsInstance := &TLS{Service: tt.service, Ca: tt.ca}
fmt.Println("Initial TLS struct:", t) //Debug
fmt.Println("Initial Deployment state:", d) //Debug
tlsInstance.AddTLSToDeployment(d)

kubeDeployment := d.GetDeployment()
fmt.Printf("Deployment after TLS addition: %+v\n", kubeDeployment) //Debug
h := &mockHelper{
Helper: &helper.Helper{},
}

fmt.Println("Volumes before addition:", d.GetDeployment().Spec.Template.Spec.Volumes) //Debug
tls := &TLS{
Service: &Service{SecretName: "test-service-secret"},
Ca: &Ca{CaSecretName: "test-ca-secret"},
}

actualMountsLen := len(kubeDeployment.Spec.Template.Spec.Containers[0].VolumeMounts)
if actualMountsLen != tt.wantMountsLen {
t.Errorf("AddTlsToDeployment() got = %v volume mounts, want %v volume mounts", actualMountsLen, tt.wantMountsLen)
}
err := tls.UpdateDeploymentWithTLS(context.TODO(), d, h)
if err != nil {
t.Fatalf("Expected no error, but got: %v", err)
}

actualVolLen := len(kubeDeployment.Spec.Template.Spec.Volumes)
if actualVolLen != tt.wantVolLen {
t.Errorf("AddTlsToDeployment() got = %v volumes, want %v volumes", actualVolLen, tt.wantVolLen)
}
})
// Add assertions as needed, for example:
if len(d.deployment.Spec.Template.Spec.Volumes) == 0 {
t.Fatalf("Expected Volumes to be updated, but got none.")
}
}

0 comments on commit 781b3cc

Please sign in to comment.