Skip to content

Commit

Permalink
Provisioner: Add support to fetch kbs pod ip
Browse files Browse the repository at this point in the history
Fixes: #1471
Signed-off-by: Kartik Joshi <kartikjoshi@microsoft.com>
  • Loading branch information
kartikjoshi21 committed Oct 17, 2023
1 parent 061003a commit 620a421
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 22 deletions.
2 changes: 1 addition & 1 deletion install/overlays/azure/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ configMapGenerator:
# /subscriptions/<AZURE_SUBSCRIPTION_ID>/resourceGroups/<AZURE_RESOURCE_GROUP>/providers/Microsoft.Compute/images/<AZURE_IMAGE>
- AZURE_IMAGE_ID="" #set
- SSH_USERNAME="" #set peer pod vm admin user name
- AA_KBC_PARAMS="" #set
#- DISABLECVM="" # Uncomment it if you want a generic VM
#- PAUSE_IMAGE="" # Uncomment and set if you want to use a specific pause image
#- VXLAN_PORT="" # Uncomment and set if you want to use a specific vxlan port. Defaults to 4789
#- AZURE_INSTANCE_SIZES="" # comma separated
#- TAGS="" # Uncomment and add key1=value1,key2=value2 etc if you want to use specific tags for podvm
#- DISABLE_CLOUD_CONFIG="" # Uncomment if you want to enable user data for podvm
#- AA_KBC_PARAMS="" # Uncomment and set if you want to set KBC params for podvm
##TLS_SETTINGS
#- CACERT_FILE="/etc/certificates/ca.crt" # for TLS
#- CERT_FILE="/etc/certificates/client.crt" # for TLS
Expand Down
30 changes: 20 additions & 10 deletions test/e2e/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package e2e

import (
"fmt"
"context"
"os"
"testing"
Expand All @@ -14,10 +15,10 @@ import (
)

var (
testEnv env.Environment
cloudProvider string
provisioner pv.CloudProvisioner
cloudAPIAdaptor *pv.CloudAPIAdaptor
testEnv env.Environment
cloudProvider string
provisioner pv.CloudProvisioner
cloudAPIAdaptor *pv.CloudAPIAdaptor
keyBrokerService *pv.KeyBrokerService
)

Expand Down Expand Up @@ -71,9 +72,6 @@ func TestMain(m *testing.M) {
// the VPC images storage.
podvmImage := os.Getenv("TEST_PODVM_IMAGE")

kbsImage := os.Getenv("TEST_KBS_IMAGE")
kbsImageTag := os.Getenv("TEST_KBS_IMAGE_TAG")

// The TEST_PROVISION_FILE is an optional variable which specifies the path
// to the provision properties file. The file must have the format:
//
Expand Down Expand Up @@ -108,6 +106,8 @@ func TestMain(m *testing.M) {
testEnv.Setup(func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
log.Info("Do setup")
var err error
// Get properties
props := provisioner.GetProperties(ctx, cfg)

if shouldProvisionCluster {
log.Info("Cluster provisioning")
Expand All @@ -122,14 +122,24 @@ func TestMain(m *testing.M) {

if shouldDeployKbs {
log.Info("Deploying kbs")
if props["KBS_IMAGE"] == "" || props["KBS_IMAGE_TAG"] == "" {
return ctx, fmt.Errorf("kbs image not provided")
}

if keyBrokerService, err = pv.NewKeyBrokerService(cloudProvider); err != nil {
if keyBrokerService, err = pv.NewKeyBrokerService(props["CLUSTER_NAME"]); err != nil {
return ctx, err
}

if err = keyBrokerService.Deploy(ctx, kbsImage, kbsImageTag); err != nil {
if err = keyBrokerService.Deploy(ctx, props["KBS_IMAGE"], props["KBS_IMAGE_TAG"]); err != nil {
return ctx, err
}
var kbsPodIP string
if kbsPodIP, err = keyBrokerService.GetKbsPodIP(ctx, cfg); err != nil {
return ctx, err
}

kbsparams := "cc_kbc::http:" + kbsPodIP + ":8080"
props["AA_KBC_PARAMS"] = kbsparams
}

if podvmImage != "" {
Expand All @@ -143,7 +153,7 @@ func TestMain(m *testing.M) {
return ctx, err
}
log.Info("Deploy the Cloud API Adaptor")
if err = cloudAPIAdaptor.Deploy(ctx, cfg, provisioner.GetProperties(ctx, cfg)); err != nil {
if err = cloudAPIAdaptor.Deploy(ctx, cfg, props); err != nil {
return ctx, err
}
return ctx, nil
Expand Down
56 changes: 46 additions & 10 deletions test/provisioner/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ type CloudAPIAdaptor struct {
runtimeClass *nodev1.RuntimeClass // The Kata Containers runtimeclass
}

type KeyBrokerService struct {
cloudProvider string // Cloud provider
}

type KeyBrokerService struct {}

type newInstallOverlayFunc func(installDir string) (InstallOverlay, error)

Expand All @@ -70,7 +67,7 @@ type InstallOverlay interface {
Edit(ctx context.Context, cfg *envconf.Config, properties map[string]string) error
}

func NewKeyBrokerService(provider string) (*KeyBrokerService, error) {
func NewKeyBrokerService(clusterName string) (*KeyBrokerService, error) {
// Clone kbs repo
repoURL := "https://github.com/confidential-containers/kbs"
cmd := exec.Command("git", "clone", repoURL)
Expand All @@ -84,7 +81,7 @@ func NewKeyBrokerService(provider string) (*KeyBrokerService, error) {
}

// Create secret
content := []byte("This is my super secret")
content := []byte("This is my cluster name: " + clusterName)
filePath := "kbs/config/kubernetes/overlays/key.bin"
// Create the file.
file, err := os.Create(filePath)
Expand All @@ -101,9 +98,7 @@ func NewKeyBrokerService(provider string) (*KeyBrokerService, error) {
return nil, err
}

return &KeyBrokerService{
cloudProvider: provider,
}, nil
return &KeyBrokerService{}, nil
}

func NewCloudAPIAdaptor(provider string, installDir string) (*CloudAPIAdaptor, error) {
Expand Down Expand Up @@ -187,6 +182,47 @@ func UpdateKbsKustomizationFile(imagePath string, imageTag string) error {

}

func (p *KeyBrokerService) GetKbsPodIP(ctx context.Context, cfg *envconf.Config) (string, error) {
client, err := cfg.NewClient()
if err != nil {
return "", err
}

namespace := "coco-tenant"
deploymentName := "kbs"

err = AllPodsRunning(ctx, cfg, namespace)
if err != nil {
fmt.Printf("All pods are not running: %v\n", err)
return "", err
}

resources := client.Resources(namespace)

podList := &corev1.PodList{}
err = resources.List(context.TODO(), podList)
if err != nil {
fmt.Printf("Error listing pods: %v\n", err)
return "", err
}

var matchingPod *corev1.Pod
for i := range podList.Items {
pod := &podList.Items[i]
if pod.Labels["app"] == deploymentName {
matchingPod = pod
break
}
}

if matchingPod == nil {
return "", fmt.Errorf("No pod with label selector found")
}

fmt.Printf("Pod IP: %s\n", matchingPod.Status.PodIP)
return matchingPod.Status.PodIP, nil
}

func (p *KeyBrokerService) Deploy(ctx context.Context, imagePath string, imageTag string) error {
originalDir, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -220,7 +256,7 @@ func (p *KeyBrokerService) Deploy(ctx context.Context, imagePath string, imageTa
keyFile := filepath.Join(k8sCnfDir, "overlays/key.bin")
if _, err := os.Stat(keyFile); os.IsNotExist(err) {
fmt.Println("key.bin file does not exist")
//return err
return err
}

kbsCert := filepath.Join(k8sCnfDir, "base/kbs.pem")
Expand Down
4 changes: 3 additions & 1 deletion test/provisioner/provision_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ func (p *AzureCloudProvisioner) GetProperties(ctx context.Context, cfg *envconf.
"AZURE_IMAGE_ID": AzureProps.ImageID,
"AZURE_SUBNET_ID": AzureProps.SubnetID,
"AZURE_INSTANCE_SIZE": AzureProps.InstanceSize,
"KBS_IMAGE": AzureProps.KbsImage,
"KBS_IMAGE_TAG": AzureProps.KbsImageTag,
}

return props
Expand All @@ -374,7 +376,7 @@ func (p *AzureCloudProvisioner) UploadPodvm(imagePath string, ctx context.Contex

func isAzureKustomizeConfigMapKey(key string) bool {
switch key {
case "CLOUD_PROVIDER", "AZURE_SUBSCRIPTION_ID", "AZURE_REGION", "AZURE_INSTANCE_SIZE", "AZURE_RESOURCE_GROUP", "AZURE_SUBNET_ID", "AZURE_IMAGE_ID", "SSH_USERNAME":
case "CLOUD_PROVIDER", "AZURE_SUBSCRIPTION_ID", "AZURE_REGION", "AZURE_INSTANCE_SIZE", "AZURE_RESOURCE_GROUP", "AZURE_SUBNET_ID", "AZURE_IMAGE_ID", "SSH_USERNAME", "AA_KBC_PARAMS":
return true
default:
return false
Expand Down
2 changes: 2 additions & 0 deletions test/provisioner/provision_azure.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ AZURE_IMAGE_ID=""
SSH_USERNAME=""
AZURE_CLI_AUTH="false"
IS_CI_MANAGED_CLUSTER="false"
KBS_IMAGE=""
KBS_IMAGE_TAG="latest"
4 changes: 4 additions & 0 deletions test/provisioner/provision_azure_initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type AzureProperties struct {
ManagedIdentityName string
IsCIManaged bool
CaaImage string
KbsImage string
KbsImageTag string

InstanceSize string
NodeName string
Expand Down Expand Up @@ -63,6 +65,8 @@ func initAzureProperties(properties map[string]string) error {
SshUserName: properties["SSH_USERNAME"],
ManagedIdentityName: properties["MANAGED_IDENTITY_NAME"],
CaaImage: properties["CAA_IMAGE"],
KbsImage: properties["KBS_IMAGE"],
KbsImageTag: properties["KBS_IMAGE_TAG"],
}

CIManagedStr := properties["IS_CI_MANAGED_CLUSTER"]
Expand Down

0 comments on commit 620a421

Please sign in to comment.