Skip to content

Commit 2f02d44

Browse files
committed
Rewrite fetch_manifest.sh into golang
Signed-off-by: peppi-lotta <[email protected]>
1 parent 2e3b5c1 commit 2f02d44

File tree

3 files changed

+97
-24
lines changed

3 files changed

+97
-24
lines changed

test/e2e/logcollector.go

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os/exec"
1010
"path"
1111
"path/filepath"
12+
"strings"
1213

1314
. "github.com/onsi/ginkgo/v2"
1415
. "github.com/onsi/gomega"
@@ -98,8 +99,81 @@ func (Metal3LogCollector) CollectMachinePoolLog(_ context.Context, _ client.Clie
9899
return fmt.Errorf("CollectMachinePoolLog not implemented")
99100
}
100101

101-
func FetchManifests(_ framework.ClusterProxy) error {
102-
return fmt.Errorf("FetchManifests not implemented")
102+
func FetchManifests(clusterProxy framework.ClusterProxy, outputPath string) error {
103+
ctx := context.Background()
104+
105+
baseDir := filepath.Join(outputPath, clusterProxy.GetName())
106+
err := os.MkdirAll(baseDir, 0775)
107+
if err != nil {
108+
return fmt.Errorf("couldn't create directory: %v", err)
109+
}
110+
111+
manifests := []string{
112+
"bmh",
113+
"hardwaredata",
114+
"cluster",
115+
"deployment",
116+
"machine",
117+
"machinedeployment",
118+
"machinehealthchecks",
119+
"machinesets",
120+
"machinepools",
121+
"m3cluster",
122+
"m3machine",
123+
"metal3machinetemplate",
124+
"kubeadmconfig",
125+
"kubeadmconfigtemplates",
126+
"kubeadmcontrolplane",
127+
"replicaset",
128+
"ippool",
129+
"ipclaim",
130+
"ipaddress",
131+
"m3data",
132+
"m3dataclaim",
133+
"m3datatemplate",
134+
}
135+
clientset := clusterProxy.GetClientSet()
136+
137+
// Get all namespaces
138+
namespaces, err := clientset.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
139+
if err != nil {
140+
return fmt.Errorf("couldn't get namespaces: %v", err)
141+
}
142+
143+
for _, namespace := range namespaces.Items {
144+
for _, kind := range manifests {
145+
// Get names of the objects whose kind and namespace match
146+
names, err := exec.Command("kubectl", "--kubeconfig", clusterProxy.GetKubeconfigPath(), "get", "-n", namespace.Name, "-o", "name", kind).Output() // #nosec G204:gosec
147+
if err != nil {
148+
return fmt.Errorf("couldn't get names: %v", err)
149+
}
150+
namesList := strings.Split(string(names), "\n")
151+
for _, name := range namesList {
152+
if name != "" {
153+
// Get yaml of each obeject
154+
yaml, err := exec.Command("kubectl", "--kubeconfig", clusterProxy.GetKubeconfigPath(), "get", "-n", namespace.Name, "-o", "yaml", name).Output() // #nosec G204:gosec
155+
if err != nil {
156+
return fmt.Errorf("couldn't get yaml: %v", err)
157+
}
158+
// Create manifest directory and any missing directory in
159+
// the path
160+
manifestDir := filepath.Join(baseDir, namespace.Name, kind)
161+
err = os.MkdirAll(manifestDir, 0775)
162+
if err != nil {
163+
return fmt.Errorf("couldn't create directory: %v", err)
164+
}
165+
// Print manifest to file
166+
file := filepath.Join(manifestDir, "manifest.yaml")
167+
err = os.WriteFile(file, []byte(yaml), 0600)
168+
if err != nil {
169+
fmt.Printf("couldn't write to file: %v", err)
170+
continue
171+
}
172+
}
173+
}
174+
}
175+
}
176+
return nil
103177
}
104178

105179
func FetchClusterLogs(clusterProxy framework.ClusterProxy, outputPath string) error {

test/e2e/pivoting.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
. "github.com/onsi/gomega"
1717
corev1 "k8s.io/api/core/v1"
1818
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19+
"k8s.io/apimachinery/pkg/runtime"
1920
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2021
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
2122
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
@@ -88,13 +89,10 @@ func pivoting(ctx context.Context, inputGetter func() PivotingInput) {
8889
}
8990

9091
By("Fetch manifest for bootstrap cluster before pivot")
91-
kconfigPathBootstrap := input.BootstrapClusterProxy.GetKubeconfigPath()
92-
os.Setenv("KUBECONFIG_BOOTSTRAP", kconfigPathBootstrap)
93-
path := filepath.Join(os.Getenv("CAPM3PATH"), "scripts")
94-
cmd := exec.Command("./fetch_manifests.sh") // #nosec G204:gosec
95-
cmd.Dir = path
96-
_ = cmd.Run()
97-
92+
err = FetchManifests(input.BootstrapClusterProxy, "/tmp/manifests/")
93+
if err != nil {
94+
fmt.Printf("Error fetching manifests for bootstrap cluster before pivot: %v\n", err)
95+
}
9896
By("Fetch target cluster kubeconfig for target cluster log collection")
9997
kconfigPathWorkload := input.TargetCluster.GetKubeconfigPath()
10098
os.Setenv("KUBECONFIG_WORKLOAD", kconfigPathWorkload)
@@ -103,7 +101,7 @@ func pivoting(ctx context.Context, inputGetter func() PivotingInput) {
103101
// target log collection. There is possibility to handle the kubeconfig in better way.
104102
// KubeconfigPathTemp will be used by project-infra target log collection only incase of failed e2e test
105103
kubeconfigPathTemp := "/tmp/kubeconfig-test1.yaml"
106-
cmd = exec.Command("cp", kconfigPathWorkload, kubeconfigPathTemp) // #nosec G204:gosec
104+
cmd := exec.Command("cp", kconfigPathWorkload, kubeconfigPathTemp) // #nosec G204:gosec
107105
stdoutStderr, er := cmd.CombinedOutput()
108106
Logf("%s\n", stdoutStderr)
109107
Expect(er).ToNot(HaveOccurred(), "Cannot fetch target cluster kubeconfig")
@@ -436,10 +434,11 @@ func rePivoting(ctx context.Context, inputGetter func() RePivotingInput) {
436434
}
437435

438436
By("Fetch manifest for workload cluster after pivot")
439-
path := filepath.Join(os.Getenv("CAPM3PATH"), "scripts")
440-
cmd := exec.Command("./fetch_manifests.sh") // #nosec G204:gosec
441-
cmd.Dir = path
442-
_ = cmd.Run()
437+
workloadClusterProxy := framework.NewClusterProxy("workload-cluster-after-pivot", os.Getenv("KUBECONFIG"), runtime.NewScheme())
438+
err = FetchManifests(workloadClusterProxy, "/tmp/manifests/")
439+
if err != nil {
440+
fmt.Printf("Error fetching manifests for workload cluster after pivot: %v\n", err)
441+
}
443442
os.Unsetenv("KUBECONFIG_WORKLOAD")
444443

445444
By("Remove Ironic deployment from target cluster")
@@ -563,10 +562,10 @@ func rePivoting(ctx context.Context, inputGetter func() RePivotingInput) {
563562
})
564563

565564
By("Fetch manifest for bootstrap cluster after re-pivot")
566-
path = filepath.Join(os.Getenv("CAPM3PATH"), "scripts")
567-
cmd = exec.Command("./fetch_manifests.sh") // #nosec G204:gosec
568-
cmd.Dir = path
569-
_ = cmd.Run()
565+
err = FetchManifests(input.BootstrapClusterProxy, "/tmp/manifests/")
566+
if err != nil {
567+
fmt.Printf("Error fetching manifests for bootstrap cluster before pivot: %v\n", err)
568+
}
570569
os.Unsetenv("KUBECONFIG_BOOTSTRAP")
571570

572571
By("RE-PIVOTING TEST PASSED!")

test/e2e/upgrade_clusterctl_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,17 @@ func preInitFunc(clusterProxy framework.ClusterProxy, bmoRelease string, ironicR
200200
}
201201

202202
By("Fetch manifest for bootstrap cluster")
203-
path := filepath.Join(os.Getenv("CAPM3PATH"), "scripts")
204-
cmd := exec.Command("./fetch_manifests.sh") // #nosec G204:gosec
205-
cmd.Dir = path
206-
_ = cmd.Run()
203+
err := FetchManifests(clusterProxy, "/tmp/manifests/")
204+
if err != nil {
205+
fmt.Printf("Error fetching manifests for bootstrap cluster: %v\n", err)
206+
}
207207

208208
By("Fetch target cluster kubeconfig for target cluster log collection")
209209
kconfigPathWorkload := clusterProxy.GetKubeconfigPath()
210210
os.Setenv("KUBECONFIG_WORKLOAD", kconfigPathWorkload)
211211
Logf("Save kubeconfig in temp folder for project-infra target log collection")
212212
kubeconfigPathTemp := "/tmp/kubeconfig-test1.yaml"
213-
cmd = exec.Command("cp", kconfigPathWorkload, kubeconfigPathTemp) // #nosec G204:gosec
213+
cmd := exec.Command("cp", kconfigPathWorkload, kubeconfigPathTemp) // #nosec G204:gosec
214214
stdoutStderr, er := cmd.CombinedOutput()
215215
Logf("%s\n", stdoutStderr)
216216
Expect(er).ToNot(HaveOccurred(), "Cannot fetch target cluster kubeconfig")
@@ -238,7 +238,7 @@ func preInitFunc(clusterProxy framework.ClusterProxy, bmoRelease string, ironicR
238238
ironicKustomizePath := fmt.Sprintf("IRONIC_RELEASE_%s", ironicRelease)
239239
initIronicKustomization := e2eConfig.GetVariable(ironicKustomizePath)
240240
By(fmt.Sprintf("Installing Ironic from kustomization %s on the upgrade cluster", initIronicKustomization))
241-
err := BuildAndApplyKustomization(ctx, &BuildAndApplyKustomizationInput{
241+
err = BuildAndApplyKustomization(ctx, &BuildAndApplyKustomizationInput{
242242
Kustomization: initIronicKustomization,
243243
ClusterProxy: clusterProxy,
244244
WaitForDeployment: true,

0 commit comments

Comments
 (0)