Skip to content

Commit

Permalink
Merge pull request metal3-io#1139 from Nordix/gosec-minor/adil
Browse files Browse the repository at this point in the history
🌱 CRD labeling is done now by k8s api to fix gosec issues
  • Loading branch information
metal3-io-bot authored Sep 26, 2023
2 parents 12e50c9 + 422ac09 commit 5318877
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 59 deletions.
4 changes: 2 additions & 2 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 //indirect
k8s.io/apiextensions-apiserver v0.27.2 // indirect
k8s.io/component-base v0.27.2 // indirect
k8s.io/apiextensions-apiserver v0.27.6 // indirect
k8s.io/component-base v0.27.6 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
8 changes: 4 additions & 4 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,14 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
k8s.io/api v0.27.6 h1:PBWu/lywJe2qQcshMjubzcBg7+XDZOo7O8JJAWuYtUo=
k8s.io/api v0.27.6/go.mod h1:AQYj0UsFCp3qJE7bOVnUuy4orCsXVkvHefnbYQiNWgk=
k8s.io/apiextensions-apiserver v0.27.2 h1:iwhyoeS4xj9Y7v8YExhUwbVuBhMr3Q4bd/laClBV6Bo=
k8s.io/apiextensions-apiserver v0.27.2/go.mod h1:Oz9UdvGguL3ULgRdY9QMUzL2RZImotgxvGjdWRq6ZXQ=
k8s.io/apiextensions-apiserver v0.27.6 h1:mOwSBJtThZhpJr+8gEkc3wFDIjq87E3JspR5mtZxIg8=
k8s.io/apiextensions-apiserver v0.27.6/go.mod h1:AVNlLYRrESG5Poo6ASRUhY2pvoKPcNt8y/IuZ4lx3o8=
k8s.io/apimachinery v0.27.6 h1:mGU8jmBq5o8mWBov+mLjdTBcU+etTE19waies4AQ6NE=
k8s.io/apimachinery v0.27.6/go.mod h1:XNfZ6xklnMCOGGFNqXG7bUrQCoR04dh/E7FprV6pb+E=
k8s.io/client-go v0.27.6 h1:vzI8804gpUtpMCNaFjIFyJrifH7u//LJCJPy8fQuYQg=
k8s.io/client-go v0.27.6/go.mod h1:PMsXcDKiJTW7PHJ64oEsIUJF319wm+EFlCj76oE5QXM=
k8s.io/component-base v0.27.2 h1:neju+7s/r5O4x4/txeUONNTS9r1HsPbyoPBAtHsDCpo=
k8s.io/component-base v0.27.2/go.mod h1:5UPk7EjfgrfgRIuDBFtsEFAe4DAvP3U+M8RTzoSJkpo=
k8s.io/component-base v0.27.6 h1:hF5WxX7Tpi9/dXAbLjPVkIA6CA6Pi6r9JOHyo0uCDYI=
k8s.io/component-base v0.27.6/go.mod h1:NvjLtaneUeb0GgMPpCBF+4LNB9GuhDHi16uUTjBhQfU=
k8s.io/klog/v2 v2.90.1 h1:m4bYOKall2MmOiRaR1J+We67Do7vm9KiQVlT96lnHUw=
k8s.io/klog/v2 v2.90.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f h1:2kWPakN3i/k81b0gvD5C5FJ2kxm1WrQFanWchyKuqGg=
Expand Down
24 changes: 24 additions & 0 deletions test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
. "github.com/onsi/gomega"
"golang.org/x/crypto/ssh"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -745,3 +746,26 @@ func (Metal3LogCollector) CollectMachinePoolLog(_ context.Context, _ client.Clie
func (Metal3LogCollector) CollectInfrastructureLogs(_ context.Context, _ client.Client, _ *clusterv1.Cluster, _ string) error {
return fmt.Errorf("CollectInfrastructureLogs not implemented")
}

// LabelCRD is adding the specified labels to the CRD crdName. Existing labels with matching keys will be overwritten.
func LabelCRD(ctx context.Context, c client.Client, crdName string, labels map[string]string) error {
crd := &apiextensionsv1.CustomResourceDefinition{}
err := c.Get(ctx, client.ObjectKey{Name: crdName}, crd)
if err != nil {
return err
}
// Apply labels to the CRD
if crd.Labels == nil {
crd.Labels = make(map[string]string)
}
for key, value := range labels {
crd.Labels[key] = value
}
// Update the CRD
err = c.Update(ctx, crd)
if err != nil {
return err
}
Logf("CRD '%s' labeled successfully\n", crdName)
return nil
}
61 changes: 17 additions & 44 deletions test/e2e/pivoting.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
dockerTypes "github.com/docker/docker/api/types"
containertypes "github.com/docker/docker/api/types/container"
docker "github.com/docker/docker/client"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
framework "sigs.k8s.io/cluster-api/test/framework"
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
Expand Down Expand Up @@ -143,9 +144,9 @@ func pivoting(ctx context.Context, inputGetter func() PivotingInput) {
LogFromFile(filepath.Join(input.ArtifactFolder, "clusters", input.ClusterName+"-pivoting", "clusterctl-init.log"))

By("Add labels to BMO CRDs")
labelBMOCRDs(nil)
labelBMOCRDs(ctx, input.BootstrapClusterProxy)
By("Add Labels to hardwareData CRDs")
labelHDCRDs(nil)
labelHDCRDs(ctx, input.BootstrapClusterProxy)

By("Install Ironic in the target cluster")
installIronicBMO(ctx, func() installIronicBMOInput {
Expand Down Expand Up @@ -186,9 +187,9 @@ func pivoting(ctx context.Context, inputGetter func() PivotingInput) {
})

By("Add labels to BMO CRDs in the target cluster")
labelBMOCRDs(input.TargetCluster)
labelBMOCRDs(ctx, input.TargetCluster)
By("Add Labels to hardwareData CRDs in the target cluster")
labelHDCRDs(input.TargetCluster)
labelHDCRDs(ctx, input.TargetCluster)
By("Ensure API servers are stable before doing move")
// Nb. This check was introduced to prevent doing move to self-hosted in an aggressive way and thus avoid flakes.
// More specifically, we were observing the test failing to get objects from the API server during move, so we
Expand Down Expand Up @@ -392,50 +393,22 @@ func RemoveDeployment(ctx context.Context, inputGetter func() RemoveDeploymentIn
Expect(err).To(BeNil(), "Failed to delete %s Deployment", deploymentName)
}

func labelBMOCRDs(targetCluster framework.ClusterProxy) {
labels := []string{
"clusterctl.cluster.x-k8s.io=",
"cluster.x-k8s.io/provider=metal3",
}
kubectlArgs := ""
if targetCluster != nil {
kubectlArgs = fmt.Sprintf("--kubeconfig=%s", targetCluster.GetKubeconfigPath())
}

func labelBMOCRDs(ctx context.Context, targetCluster framework.ClusterProxy) {
labels := map[string]string{}
labels[clusterctlv1.ClusterctlLabel] = ""
labels[clusterv1.ProviderNameLabel] = "metal3"
crdName := "baremetalhosts.metal3.io"
for _, label := range labels {
var cmd *exec.Cmd
if kubectlArgs == "" {
cmd = exec.Command("kubectl", "label", "--overwrite", "crds", crdName, label) //#nosec G204:gosec
} else {
cmd = exec.Command("kubectl", kubectlArgs, "label", "--overwrite", "crds", crdName, label) //#nosec G204:gosec
}
err := cmd.Run()
Expect(err).To(BeNil(), "Cannot label BMO CRDs")
}
err := LabelCRD(ctx, targetCluster.GetClient(), crdName, labels)
Expect(err).To(BeNil(), "Cannot label BMH CRDs")
}

func labelHDCRDs(targetCluster framework.ClusterProxy) {
labels := []string{
"clusterctl.cluster.x-k8s.io=",
"clusterctl.cluster.x-k8s.io/move=",
}
kubectlArgs := ""
if targetCluster != nil {
kubectlArgs = fmt.Sprintf("--kubeconfig=%s", targetCluster.GetKubeconfigPath())
}

func labelHDCRDs(ctx context.Context, targetCluster framework.ClusterProxy) {
labels := map[string]string{}
labels[clusterctlv1.ClusterctlLabel] = ""
labels[clusterctlv1.ClusterctlMoveLabel] = ""
crdName := "hardwaredata.metal3.io"
for _, label := range labels {
var cmd *exec.Cmd
if kubectlArgs == "" {
cmd = exec.Command("kubectl", "label", "--overwrite", "crds", crdName, label) // #nosec G204:gosec
} else {
cmd = exec.Command("kubectl", kubectlArgs, "label", "--overwrite", "crds", crdName, label) // #nosec G204:gosec
}
err := cmd.Run()
Expect(err).To(BeNil(), "Cannot label HD CRDs")
}
err := LabelCRD(ctx, targetCluster.GetClient(), crdName, labels)
Expect(err).To(BeNil(), "Cannot label HD CRDs")
}

type RePivotingInput struct {
Expand Down
6 changes: 3 additions & 3 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
golang.org/x/crypto v0.13.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.27.6
k8s.io/apiextensions-apiserver v0.27.6
k8s.io/apimachinery v0.27.6
k8s.io/client-go v0.27.6
k8s.io/utils v0.0.0-20230209194617-a36077c30491
Expand Down Expand Up @@ -120,10 +121,9 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gotest.tools/v3 v3.5.0 // indirect
k8s.io/apiextensions-apiserver v0.27.2 // indirect
k8s.io/apiserver v0.27.2 // indirect
k8s.io/apiserver v0.27.6 // indirect
k8s.io/cluster-bootstrap v0.27.2 // indirect
k8s.io/component-base v0.27.2 // indirect
k8s.io/component-base v0.27.6 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
12 changes: 6 additions & 6 deletions test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -878,18 +878,18 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/api v0.27.6 h1:PBWu/lywJe2qQcshMjubzcBg7+XDZOo7O8JJAWuYtUo=
k8s.io/api v0.27.6/go.mod h1:AQYj0UsFCp3qJE7bOVnUuy4orCsXVkvHefnbYQiNWgk=
k8s.io/apiextensions-apiserver v0.27.2 h1:iwhyoeS4xj9Y7v8YExhUwbVuBhMr3Q4bd/laClBV6Bo=
k8s.io/apiextensions-apiserver v0.27.2/go.mod h1:Oz9UdvGguL3ULgRdY9QMUzL2RZImotgxvGjdWRq6ZXQ=
k8s.io/apiextensions-apiserver v0.27.6 h1:mOwSBJtThZhpJr+8gEkc3wFDIjq87E3JspR5mtZxIg8=
k8s.io/apiextensions-apiserver v0.27.6/go.mod h1:AVNlLYRrESG5Poo6ASRUhY2pvoKPcNt8y/IuZ4lx3o8=
k8s.io/apimachinery v0.27.6 h1:mGU8jmBq5o8mWBov+mLjdTBcU+etTE19waies4AQ6NE=
k8s.io/apimachinery v0.27.6/go.mod h1:XNfZ6xklnMCOGGFNqXG7bUrQCoR04dh/E7FprV6pb+E=
k8s.io/apiserver v0.27.2 h1:p+tjwrcQEZDrEorCZV2/qE8osGTINPuS5ZNqWAvKm5E=
k8s.io/apiserver v0.27.2/go.mod h1:EsOf39d75rMivgvvwjJ3OW/u9n1/BmUMK5otEOJrb1Y=
k8s.io/apiserver v0.27.6 h1:r/eHN8r3lG2buggHrVMy++kKhHlHn1HWSX1dqDtes54=
k8s.io/apiserver v0.27.6/go.mod h1:Xeo9OEXn2kDLK5pqspjdXQx7YKgDyKSpwIB4p0BmpAQ=
k8s.io/client-go v0.27.6 h1:vzI8804gpUtpMCNaFjIFyJrifH7u//LJCJPy8fQuYQg=
k8s.io/client-go v0.27.6/go.mod h1:PMsXcDKiJTW7PHJ64oEsIUJF319wm+EFlCj76oE5QXM=
k8s.io/cluster-bootstrap v0.27.2 h1:OL3onrOwrUD7NQxBUqQwTl1Uu2GQKCkw9BMHpc4PbiA=
k8s.io/cluster-bootstrap v0.27.2/go.mod h1:b++PF0mjUOiTKdPQFlDw7p4V2VquANZ8SfhAwzxZJFM=
k8s.io/component-base v0.27.2 h1:neju+7s/r5O4x4/txeUONNTS9r1HsPbyoPBAtHsDCpo=
k8s.io/component-base v0.27.2/go.mod h1:5UPk7EjfgrfgRIuDBFtsEFAe4DAvP3U+M8RTzoSJkpo=
k8s.io/component-base v0.27.6 h1:hF5WxX7Tpi9/dXAbLjPVkIA6CA6Pi6r9JOHyo0uCDYI=
k8s.io/component-base v0.27.6/go.mod h1:NvjLtaneUeb0GgMPpCBF+4LNB9GuhDHi16uUTjBhQfU=
k8s.io/klog/v2 v2.90.1 h1:m4bYOKall2MmOiRaR1J+We67Do7vm9KiQVlT96lnHUw=
k8s.io/klog/v2 v2.90.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f h1:2kWPakN3i/k81b0gvD5C5FJ2kxm1WrQFanWchyKuqGg=
Expand Down

0 comments on commit 5318877

Please sign in to comment.