Skip to content

Commit

Permalink
Rename deployment package to managedcluster
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Squizzato <[email protected]>
  • Loading branch information
squizzi committed Sep 5, 2024
1 parent ca01d18 commit 1ae09c4
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 43 deletions.
45 changes: 22 additions & 23 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"

"github.com/Mirantis/hmc/test/deployment"
"github.com/Mirantis/hmc/test/kubeclient"
"github.com/Mirantis/hmc/test/managedcluster"
"github.com/Mirantis/hmc/test/utils"
)

Expand Down Expand Up @@ -67,13 +67,13 @@ var _ = Describe("controller", Ordered, func() {
return err
}

for _, provider := range []deployment.ProviderType{
deployment.ProviderCAPI,
deployment.ProviderAWS,
deployment.ProviderAzure,
for _, provider := range []managedcluster.ProviderType{
managedcluster.ProviderCAPI,
managedcluster.ProviderAWS,
managedcluster.ProviderAzure,
} {
// Ensure only one controller pod is running.
if err := verifyControllerUp(kc, deployment.GetProviderLabel(provider), string(provider)); err != nil {
if err := verifyControllerUp(kc, managedcluster.GetProviderLabel(provider), string(provider)); err != nil {
return err
}
}
Expand Down Expand Up @@ -112,7 +112,7 @@ var _ = Describe("controller", Ordered, func() {
// as well as the output of clusterctl to store as artifacts.
if CurrentSpecReport().Failed() {
By("collecting failure logs from controllers")
collectLogArtifacts(kc, clusterName, deployment.ProviderAWS, deployment.ProviderCAPI)
collectLogArtifacts(kc, clusterName, managedcluster.ProviderAWS, managedcluster.ProviderCAPI)
}

// Delete the deployments if they were created.
Expand All @@ -132,30 +132,33 @@ var _ = Describe("controller", Ordered, func() {
ExpectWithOffset(2, err).NotTo(HaveOccurred())
})

for _, template := range []deployment.Template{deployment.TemplateAWSStandaloneCP, deployment.TemplateAWSHostedCP} {
for _, template := range []managedcluster.Template{
managedcluster.TemplateAWSStandaloneCP,
managedcluster.TemplateAWSHostedCP,
} {
It(fmt.Sprintf("should work with an AWS provider and %s template", template), func() {
if template == deployment.TemplateAWSHostedCP {
if template == managedcluster.TemplateAWSHostedCP {
// TODO: Create AWS resources for hosted control plane.
Skip("AWS hosted control plane not yet implemented")
}

By("creating a Deployment")
d := deployment.GetUnstructuredDeployment(deployment.ProviderAWS, template)
d := managedcluster.GetUnstructured(managedcluster.ProviderAWS, template)
clusterName = d.GetName()

deleteFunc, err = kc.CreateDeployment(context.Background(), d)
Expect(err).NotTo(HaveOccurred())

By("waiting for infrastructure providers to deploy successfully")
Eventually(func() error {
return deployment.VerifyProviderDeployed(context.Background(), kc, clusterName)
return managedcluster.VerifyProviderDeployed(context.Background(), kc, clusterName)
}).WithTimeout(30 * time.Minute).WithPolling(10 * time.Second).Should(Succeed())

By("verify the deployment deletes successfully")
err = deleteFunc()
Expect(err).NotTo(HaveOccurred())
Eventually(func() error {
return deployment.VerifyProviderDeleted(context.Background(), kc, clusterName)
return managedcluster.VerifyProviderDeleted(context.Background(), kc, clusterName)
}).WithTimeout(10 * time.Minute).WithPolling(10 * time.Second).Should(Succeed())
})
}
Expand Down Expand Up @@ -197,13 +200,13 @@ func verifyControllerUp(kc *kubeclient.KubeClient, labelSelector string, name st
// CAPI controller and the provider controller(s) as well as output from clusterctl
// and stores them in the test/e2e directory as artifacts. If it fails it
// produces a warning message to the GinkgoWriter, but does not fail the test.
func collectLogArtifacts(kc *kubeclient.KubeClient, clusterName string, providerTypes ...deployment.ProviderType) {
func collectLogArtifacts(kc *kubeclient.KubeClient, clusterName string, providerTypes ...managedcluster.ProviderType) {
GinkgoHelper()

filterLabels := []string{hmcControllerLabel}

for _, providerType := range providerTypes {
filterLabels = append(filterLabels, deployment.GetProviderLabel(providerType))
filterLabels = append(filterLabels, managedcluster.GetProviderLabel(providerType))
}

for _, label := range filterLabels {
Expand All @@ -217,22 +220,22 @@ func collectLogArtifacts(kc *kubeclient.KubeClient, clusterName string, provider
})
podLogs, err := req.Stream(context.Background())
if err != nil {
warnError(fmt.Errorf("failed to get log stream for pod %s: %w", pod.Name, err))
utils.WarnError(fmt.Errorf("failed to get log stream for pod %s: %w", pod.Name, err))
continue
}
defer podLogs.Close() //nolint:errcheck

output, err := os.Create(fmt.Sprintf("./test/e2e/%s.log", pod.Name))
if err != nil {
warnError(fmt.Errorf("failed to create log file for pod %s: %w", pod.Name, err))
utils.WarnError(fmt.Errorf("failed to create log file for pod %s: %w", pod.Name, err))
continue
}
defer output.Close() //nolint:errcheck

r := bufio.NewReader(podLogs)
_, err = r.WriteTo(output)
if err != nil {
warnError(fmt.Errorf("failed to write log file for pod %s: %w", pod.Name, err))
utils.WarnError(fmt.Errorf("failed to write log file for pod %s: %w", pod.Name, err))
}
}
}
Expand All @@ -241,16 +244,12 @@ func collectLogArtifacts(kc *kubeclient.KubeClient, clusterName string, provider
"describe", "cluster", clusterName, "--namespace", namespace, "--show-conditions=all")
output, err := utils.Run(cmd)
if err != nil {
warnError(fmt.Errorf("failed to get clusterctl log: %w", err))
utils.WarnError(fmt.Errorf("failed to get clusterctl log: %w", err))
return
}

err = os.WriteFile(filepath.Join("test/e2e", "clusterctl.log"), output, 0644)
if err != nil {
warnError(fmt.Errorf("failed to write clusterctl log: %w", err))
utils.WarnError(fmt.Errorf("failed to write clusterctl log: %w", err))
}
}

func warnError(err error) {
_, _ = fmt.Fprintf(GinkgoWriter, "Warning: %v\n", err)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package deployment
package managedcluster

import (
_ "embed"
Expand Down Expand Up @@ -45,46 +45,46 @@ const (
)

//go:embed resources/aws-standalone-cp.yaml.tpl
var awsStandaloneCPDeploymentTemplateBytes []byte
var awsStandaloneCPManagedClusterTemplateBytes []byte

//go:embed resources/aws-hosted-cp.yaml.tpl
var awsHostedCPDeploymentTemplateBytes []byte
var awsHostedCPManagedClusterTemplateBytes []byte

func GetProviderLabel(provider ProviderType) string {
return fmt.Sprintf("%s=%s", providerLabel, provider)
}

// GetUnstructuredDeployment returns an unstructured deployment object based on
// the provider and template.
func GetUnstructuredDeployment(provider ProviderType, templateName Template) *unstructured.Unstructured {
// GetUnstructured returns an unstructured ManagedCluster object based on the
// provider and template.
func GetUnstructured(provider ProviderType, templateName Template) *unstructured.Unstructured {
GinkgoHelper()

generatedName := uuid.New().String()[:8] + "-e2e-test"
_, _ = fmt.Fprintf(GinkgoWriter, "Generated cluster name: %q\n", generatedName)

switch provider {
case ProviderAWS:
Expect(os.Setenv("DEPLOYMENT_NAME", generatedName)).NotTo(HaveOccurred())
Expect(os.Setenv("MANAGED_CLUSTER_NAME", generatedName)).NotTo(HaveOccurred())

var deploymentTemplateBytes []byte
var managedClusterTemplateBytes []byte
switch templateName {
case TemplateAWSStandaloneCP:
deploymentTemplateBytes = awsStandaloneCPDeploymentTemplateBytes
managedClusterTemplateBytes = awsStandaloneCPManagedClusterTemplateBytes
case TemplateAWSHostedCP:
deploymentTemplateBytes = awsHostedCPDeploymentTemplateBytes
managedClusterTemplateBytes = awsHostedCPManagedClusterTemplateBytes
default:
Fail(fmt.Sprintf("unsupported AWS template: %s", templateName))
}

deploymentConfigBytes, err := envsubst.Bytes(deploymentTemplateBytes)
managedClusterConfigBytes, err := envsubst.Bytes(managedClusterTemplateBytes)
Expect(err).NotTo(HaveOccurred(), "failed to substitute environment variables")

var deploymentConfig map[string]interface{}
var managedClusterConfig map[string]interface{}

err = yaml.Unmarshal(deploymentConfigBytes, &deploymentConfig)
err = yaml.Unmarshal(managedClusterConfigBytes, &managedClusterConfig)
Expect(err).NotTo(HaveOccurred(), "failed to unmarshal deployment config")

return &unstructured.Unstructured{Object: deploymentConfig}
return &unstructured.Unstructured{Object: managedClusterConfig}
default:
Fail(fmt.Sprintf("unsupported provider: %s", provider))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: hmc.mirantis.com/v1alpha1
kind: Deployment
kind: ManagedCluster
metadata:
name: ${DEPLOYMENT_NAME}
name: ${MANAGED_CLUSTER_NAME}
spec:
template: aws-hosted-cp
config:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: hmc.mirantis.com/v1alpha1
kind: Deployment
kind: ManagedCluster
metadata:
name: ${DEPLOYMENT_NAME}
name: ${MANAGED_CLUSTER_NAME}
spec:
template: aws-standalone-cp
config:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package deployment
package managedcluster

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package deployment
package managedcluster

import (
"context"
Expand Down
4 changes: 4 additions & 0 deletions test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,7 @@ func ValidateObjectNamePrefix(unstrObj *unstructured.Unstructured, clusterName s
func ObjKindName(unstrObj *unstructured.Unstructured) (string, string) {
return unstrObj.GetKind(), unstrObj.GetName()
}

func WarnError(err error) {
_, _ = fmt.Fprintf(GinkgoWriter, "Warning: %v\n", err)
}

0 comments on commit 1ae09c4

Please sign in to comment.