Skip to content

Commit

Permalink
Inject sidecar in PowerMax (#86)
Browse files Browse the repository at this point in the history
* merging all commmits to one to prevent having changes from main

* file changes form testing

* commit test updates (#94)

* Updated spec to next release version (#96)

* sanitize and introduced new test cases

* merging all commits to one to prevent having changes from main

* file changes form testing

* sanitize and introduced new test cases

* adds insecure flag

* removes error

* removes changes

* debugging

* fixes failing test

Co-authored-by: sharmilarama <[email protected]>
Co-authored-by: Trevor Dawe <[email protected]>
  • Loading branch information
3 people authored May 12, 2021
1 parent 8f15eef commit ab7a57d
Show file tree
Hide file tree
Showing 14 changed files with 4,696 additions and 1,546 deletions.
732 changes: 608 additions & 124 deletions cmd/karavictl/cmd/inject.go

Large diffs are not rendered by default.

246 changes: 192 additions & 54 deletions cmd/karavictl/cmd/inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,111 @@ package cmd
import (
"io/ioutil"
"net/url"
"reflect"
"testing"

corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"
)

func TestListChange(t *testing.T) {
func TestListChangePowerFlex(t *testing.T) {
// This file was generated using the following command:
// kubectl get secrets,deployments,daemonsets -n vxflexos -o yaml
b, err := ioutil.ReadFile("./testdata/kubectl_get_all_in_ns_without_karavi.yaml")
listChangeMultiArray(t, "./testdata/kubectl_get_all_in_vxflexos.yaml", "vxflexos-config", 5)
}

func TestListChangeObservability(t *testing.T) {
// This file was generated using the following command:
// kubectl get secrets,deployments -n karavi -o yaml
listChangeMultiArray(t, "./testdata/kubectl_get_all_in_karavi_observability.yaml", "vxflexos-config", 11)
}

func TestListChangePowerMaxNew(t *testing.T) {
// This file was generated BEFORE injecting sidecar by using the following command:
// kubectl get secrets,deployments,daemonsets -n powermax -o yaml

//./testdata/kubectl_get_all_in_powermax.yaml
listChangePowerMax(t, "./testdata/kubectl_get_all_in_powermax_new.yaml", 4)

}
func TestListChangePowerMaxUpdate(t *testing.T) {
// This file was generated AFTER injecting sidecar by using the following command:
// kubectl get secrets,deployments,daemonsets -n powermax -o yaml
listChangePowerMax(t, "./testdata/kubectl_get_all_in_powermax_update.yaml", 7)

}
func TestGetStartingPortRanges(t *testing.T) {
t.Run("no proxyPort flag", func(t *testing.T) {
proxyPortFlags := []string{}
got, err := getStartingPortRanges(proxyPortFlags)
if err != nil {
t.Fatal(err)
}

want := map[string]int{
"powerflex": DefaultStartingPortRange,
"powermax": DefaultStartingPortRange + 200,
}

if !reflect.DeepEqual(got, want) {
t.Errorf("got %+v, want %+v", got, want)
}
})

t.Run("custom powerflex proxy port", func(t *testing.T) {
proxyPortFlags := []string{"powerflex=10000"}
got, err := getStartingPortRanges(proxyPortFlags)
if err != nil {
t.Fatal(err)
}

want := map[string]int{
"powerflex": 10000,
"powermax": 10200,
}

if !reflect.DeepEqual(got, want) {
t.Errorf("got %+v, want %+v", got, want)
}
})

t.Run("custom powermax proxy port", func(t *testing.T) {
proxyPortFlags := []string{"powermax=10000"}
got, err := getStartingPortRanges(proxyPortFlags)
if err != nil {
t.Fatal(err)
}

want := map[string]int{
"powerflex": 9800,
"powermax": 10000,
}

if !reflect.DeepEqual(got, want) {
t.Errorf("got %+v, want %+v", got, want)
}
})

t.Run("custom powerflex and powermax proxy port", func(t *testing.T) {
proxyPortFlags := []string{"powerflex=10000", "powermax=20000"}
got, err := getStartingPortRanges(proxyPortFlags)
if err != nil {
t.Fatal(err)
}

want := map[string]int{
"powerflex": 10000,
"powermax": 20000,
}

if !reflect.DeepEqual(got, want) {
t.Errorf("got %+v, want %+v", got, want)
}
})
}

func listChangeMultiArray(t *testing.T, path, wantKey string, wantLen int) {
b, err := ioutil.ReadFile(path)
if err != nil {
t.Fatal(err)
}
Expand All @@ -36,13 +131,19 @@ func TestListChange(t *testing.T) {
t.Fatal(err)
}

sut := NewListChange(&existing)
var sut ListChangeForMultiArray
sut.ListChange = NewListChange(&existing)

t.Run("injects the proxy pieces", func(t *testing.T) {
portRanges, err := getStartingPortRanges(nil)
if err != nil {
t.Fatal(err)
}

got, err := injectUsingList(b,
"http://image-addr",
"http://proxy-addr",
"./testdata/fake-certificate-file.pem", false)
"./testdata/fake-certificate-file.pem", portRanges, false)
if err != nil {
t.Fatal(err)
}
Expand All @@ -57,57 +158,28 @@ func TestListChange(t *testing.T) {
t.Fatal(err)
}

wantLen := 9
if l := len(got); l != wantLen {
t.Errorf("buildMapOfSecretsFromList: got len %d, want %d", l, wantLen)
}
// The vxflexos-config Secret should exist with a non-nil value.
wantKey := "vxflexos-config"
// The Secret should exist with a non-nil value.
if v, ok := got[wantKey]; !ok || v == nil {
t.Errorf("buildMapOfSecretsFromList: expected key %q to exist, but got [%v,%v]",
wantKey, v, ok)
}
})
t.Run("inject a new secret with localhost endpoints", func(t *testing.T) {
sut.InjectResources = &Resources{
Secret: wantKey,
}
sut.injectKaraviSecret()
if sut.Err != nil {
t.Fatal(sut.Err)
}

// Extract only the secrets from this list.
secrets, err := buildMapOfSecretsFromList(sut.Modified)
if err != nil {
t.Fatal(err)
}
secret, ok := secrets["karavi-authorization-config"]
if !ok {
t.Fatal("expected new secret to exist, but it didn't")
}
secretData, err := getSecretData(secret)
if err != nil {
t.Fatal(err)
}
for _, v := range secretData {
u, err := url.Parse(v.Endpoint)
if err != nil {
t.Fatal(err)
}
want := "localhost"
if got := u.Hostname(); got != want {
t.Errorf("got %q, want %q", got, want)
}
}
// The new secret should be called karavi-auth-config
// It should replace endpoint values with localhost
// Each localhost should have a unique port number
// The original secret should be left intact.
})
}

func TestListChangeObservability(t *testing.T) {
// This file was generated using the following command:
// kubectl get secrets,deployments,daemonsets -n vxflexos -o yaml
b, err := ioutil.ReadFile("./testdata/kubectl_get_all_in_karavi_observability.yaml")
func listChangePowerMax(t *testing.T, path string, wantLen int) {
b, err := ioutil.ReadFile(path)
if err != nil {
t.Fatal(err)
}
Expand All @@ -117,13 +189,19 @@ func TestListChangeObservability(t *testing.T) {
t.Fatal(err)
}

sut := NewListChange(&existing)
var sut ListChangeForPowerMax
sut.ListChange = NewListChange(&existing)
wantKey := "powermax-creds"

t.Run("injects the proxy pieces", func(t *testing.T) {
portRanges, err := getStartingPortRanges(nil)
if err != nil {
t.Fatal(err)
}
got, err := injectUsingList(b,
"http://image-addr",
"http://proxy-addr",
"./testdata/fake-certificate-file.pem", false)
"./testdata/fake-certificate-file.pem", portRanges, false)
if err != nil {
t.Fatal(err)
}
Expand All @@ -137,20 +215,21 @@ func TestListChangeObservability(t *testing.T) {
if err != nil {
t.Fatal(err)
}

wantLen := 11
if l := len(got); l != wantLen {
t.Errorf("buildMapOfSecretsFromList: got len %d, want %d", l, wantLen)
}
// The vxflexos-config Secret should exist with a non-nil value.
wantKey := "vxflexos-config"
// The Secret should exist with a non-nil value.
if v, ok := got[wantKey]; !ok || v == nil {
t.Errorf("buildMapOfSecretsFromList: expected key %q to exist, but got [%v,%v]",
wantKey, v, ok)
}
})
t.Run("inject a new secret with localhost endpoints", func(t *testing.T) {
sut.injectKaraviSecret()
sut.InjectResources = &Resources{
Secret: wantKey,
Deployment: "powermax-controller",
}
sut.injectKaraviSecret(true)
if sut.Err != nil {
t.Fatal(sut.Err)
}
Expand Down Expand Up @@ -183,13 +262,72 @@ func TestListChangeObservability(t *testing.T) {
// Each localhost should have a unique port number
// The original secret should be left intact.
})
}
t.Run("inject a new deployment with localhost endpoints", func(t *testing.T) {
modified, err := sut.Change(&existing, "http://image-addr", "http://proxy-addr", "", true)
if err != nil {
t.Fatal(err)
}

func toSecret(t *testing.T, b []byte) *corev1.Secret {
var s corev1.Secret
err := yaml.Unmarshal(b, &s)
if err != nil {
t.Fatal(err)
}
return &s
m, err := buildMapOfDeploymentsFromList(modified)
if err != nil {
t.Fatal(err)
}

deploy, ok := m[sut.InjectResources.Deployment]
if !ok {
t.Fatal("deployment not found")
}

// check that endpoint is modified
for _, c := range deploy.Spec.Template.Spec.Containers {
if c.Name == "driver" {
commandEnvFlag := false
for _, e := range c.Env {
if e.Name == "X_CSI_POWERMAX_ENDPOINT" {
u, err := url.Parse(e.Value)
if err != nil {
t.Fatal(err)
}
want := "localhost"
if got := u.Hostname(); got != want {
t.Errorf("got %q, want %q", got, want)
}
commandEnvFlag = true
}

}
if !commandEnvFlag {
t.Fatal("X_CSI_POWERMAX_ENDPOINT")
}
break
}

}

for _, c := range deploy.Spec.Template.Spec.Containers {
if c.Name == "driver" {
commandEnvFlag := false
for _, e := range c.Env {
if e.Name == "CSM_CSI_POWERMAX_ENDPOINT" {
u, err := url.Parse(e.Value)
if err != nil {
t.Fatal(err)
}
want := "localhost"
if got := u.Hostname(); got == want {
t.Errorf("got %q, want %q", got, want)
}
commandEnvFlag = true
}

}
if !commandEnvFlag {
t.Fatal("CSM_CSI_POWERMAX_ENDPOINT")
}
break
}

}

})
}
Loading

0 comments on commit ab7a57d

Please sign in to comment.