From a748cb3c9cae38ba1a7023cf0624365d81e7df05 Mon Sep 17 00:00:00 2001 From: Sam Gartner Date: Mon, 18 Mar 2024 14:59:08 -0500 Subject: [PATCH] Constants update Update to remove several global constants and generally rework how we store/utilize certain constants that appear across many files in both rancher/rancher and rancher/shepherd. Removed duplicate const file apiobjects comment update Updated deflabels to default labels as per Calebs comment PR update to address comments Updated the stevetypes.Setting const to ManagementSetting for clarity Update node_status.go Update constants PR with more constants change defaults back --- clients/k3d/k3d.go | 18 ++++--- clients/rkecli/state.go | 4 +- extensions/charts/awsoutoftree.go | 20 ++++---- extensions/charts/charts.go | 15 +++--- extensions/charts/payloads.go | 9 ++-- extensions/charts/ranchergatekeeper.go | 18 +++---- extensions/charts/rancheristio.go | 16 +++--- extensions/charts/rancherlogging.go | 14 +++--- extensions/charts/ranchermonitoring.go | 18 +++---- extensions/charts/verify.go | 4 +- extensions/clusters/bundledclusters/get.go | 4 +- extensions/clusters/bundledclusters/update.go | 3 +- extensions/clusters/clusters.go | 50 +++++++++---------- extensions/clusters/import.go | 11 ++-- extensions/codecoverage/codecoverage.go | 24 ++++----- .../defaults/annotations/annotations.go | 12 +++++ extensions/defaults/labels/labels.go | 8 +++ extensions/defaults/namespaces/namespaces.go | 7 +++ extensions/defaults/stevetypes/stevetypes.go | 23 +++++++-- extensions/defaults/timeouts/timeouts.go | 21 ++++++++ extensions/etcdsnapshot/etcdsnapshot.go | 24 ++++----- extensions/ingresses/ingresses.go | 8 +-- extensions/kubeapi/namespaces/create.go | 11 ++-- extensions/kubeapi/projects/delete.go | 4 +- .../kubeapi/workloads/cronjobs/create.go | 4 +- .../kubeapi/workloads/daemonsets/create.go | 3 +- .../kubeapi/workloads/deployments/create.go | 3 +- extensions/kubeapi/workloads/jobs/create.go | 4 +- extensions/kubectl/command.go | 4 +- extensions/machinepools/machinepools.go | 13 ++--- extensions/namespaces/create.go | 14 +++--- extensions/nodes/node_status.go | 18 +++---- extensions/pipeline/setup.go | 9 ++-- extensions/provisioning/creates.go | 40 ++++++++------- extensions/provisioning/ssh.go | 11 ++-- extensions/provisioning/verify.go | 32 ++++++------ extensions/psact/createdeployment.go | 14 +++--- extensions/rancherleader/rancherleader.go | 4 +- extensions/rbac/verify.go | 16 +++--- extensions/registries/registries.go | 3 +- .../rke1/componentchecks/etcdversion.go | 9 ++-- extensions/rke1/nodepools/nodepools.go | 6 +-- extensions/services/verify.go | 3 +- extensions/settings/shell.go | 3 +- extensions/sshkeys/downloadsshkeys.go | 3 +- extensions/workloads/create.go | 5 +- extensions/workloads/pods/pod_status.go | 7 +-- extensions/workloads/pods/verify.go | 5 +- extensions/workloads/template.go | 9 ++-- extensions/workloads/verify.go | 3 +- pkg/codegen/main.go | 3 +- pkg/schemas/mapper/node_address.go | 3 +- 52 files changed, 347 insertions(+), 250 deletions(-) create mode 100644 extensions/defaults/annotations/annotations.go create mode 100644 extensions/defaults/labels/labels.go create mode 100644 extensions/defaults/namespaces/namespaces.go create mode 100644 extensions/defaults/timeouts/timeouts.go diff --git a/clients/k3d/k3d.go b/clients/k3d/k3d.go index d3b4439d..da1da270 100644 --- a/clients/k3d/k3d.go +++ b/clients/k3d/k3d.go @@ -9,7 +9,9 @@ import ( apisV1 "github.com/rancher/rancher/pkg/apis/provisioning.cattle.io/v1" "github.com/rancher/shepherd/clients/rancher" "github.com/rancher/shepherd/extensions/clusters" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/namespaces" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" "github.com/rancher/shepherd/pkg/config" "github.com/rancher/shepherd/pkg/session" "github.com/rancher/shepherd/pkg/wait" @@ -102,10 +104,10 @@ func CreateAndImportK3DCluster(client *rancher.Client, name, image, hostname str cluster := &apisV1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: name, - Namespace: "fleet-default", + Namespace: namespaces.Fleet, }, } - clusterObj, err := client.Steve.SteveType(clusters.ProvisioningSteveResourceType).Create(cluster) + clusterObj, err := client.Steve.SteveType(stevetypes.Provisioning).Create(cluster) if err != nil { return nil, errors.Wrap(err, "CreateAndImportK3DCluster: failed to create provisioning cluster") } @@ -114,7 +116,7 @@ func CreateAndImportK3DCluster(client *rancher.Client, name, image, hostname str logrus.Infof("Creating K3D cluster...") downRest, err := CreateK3DCluster(client.Session, name, hostname, servers, agents) if err != nil { - _ = client.Steve.SteveType(clusters.ProvisioningSteveResourceType).Delete(clusterObj) + _ = client.Steve.SteveType(stevetypes.Provisioning).Delete(clusterObj) return nil, errors.Wrap(err, "CreateAndImportK3DCluster: failed to create k3d cluster") } @@ -132,9 +134,9 @@ func CreateAndImportK3DCluster(client *rancher.Client, name, image, hostname str } // wait for the provisioning cluster logrus.Infof("Waiting for provisioning cluster...") - clusterWatch, err := kubeProvisioningClient.Clusters("fleet-default").Watch(context.TODO(), metav1.ListOptions{ + clusterWatch, err := kubeProvisioningClient.Clusters(namespaces.Fleet).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + name, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return nil, errors.Wrap(err, "CreateAndImportK3DCluster: failed to watch for the imported cluster") @@ -144,7 +146,7 @@ func CreateAndImportK3DCluster(client *rancher.Client, name, image, hostname str err = wait.WatchWait(clusterWatch, func(event watch.Event) (bool, error) { cluster := event.Object.(*apisV1.Cluster) if cluster.Name == name { - impCluster, err = kubeProvisioningClient.Clusters("fleet-default").Get(context.TODO(), name, metav1.GetOptions{}) + impCluster, err = kubeProvisioningClient.Clusters(namespaces.Fleet).Get(context.TODO(), name, metav1.GetOptions{}) return true, err } @@ -164,7 +166,7 @@ func CreateAndImportK3DCluster(client *rancher.Client, name, image, hostname str // wait for the imported cluster to be ready logrus.Infof("Waiting for imported cluster...") - clusterWatch, err = kubeProvisioningClient.Clusters("fleet-default").Watch(context.TODO(), metav1.ListOptions{ + clusterWatch, err = kubeProvisioningClient.Clusters(namespaces.Fleet).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + name, TimeoutSeconds: &importTimeout, }) diff --git a/clients/rkecli/state.go b/clients/rkecli/state.go index b6c1f15c..c209478a 100644 --- a/clients/rkecli/state.go +++ b/clients/rkecli/state.go @@ -12,7 +12,7 @@ import ( "github.com/rancher/shepherd/clients/rancher" v3 "github.com/rancher/shepherd/clients/rancher/generated/management/v3" v1 "github.com/rancher/shepherd/clients/rancher/v1" - "github.com/rancher/shepherd/extensions/configmaps" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" "github.com/rancher/shepherd/pkg/config" "github.com/rancher/shepherd/pkg/file" "gopkg.in/yaml.v2" @@ -169,7 +169,7 @@ func NewStateFile(state *cluster.FullState, dirName string) (stateFilePath strin // GetFullState is a function that gets RKE full state from "full-cluster-state" configmap. // And returns the cluster full state. func GetFullState(client *rancher.Client) (state *cluster.FullState, err error) { - namespacedConfigmapClient := client.Steve.SteveType(configmaps.ConfigMapSteveType).NamespacedSteveClient(cluster.SystemNamespace) + namespacedConfigmapClient := client.Steve.SteveType(stevetypes.Configmap).NamespacedSteveClient(cluster.SystemNamespace) if err != nil { return } diff --git a/extensions/charts/awsoutoftree.go b/extensions/charts/awsoutoftree.go index abd35a2b..ad36dde4 100644 --- a/extensions/charts/awsoutoftree.go +++ b/extensions/charts/awsoutoftree.go @@ -9,17 +9,15 @@ import ( "github.com/rancher/shepherd/clients/rancher" steveV1 "github.com/rancher/shepherd/clients/rancher/v1" - "github.com/rancher/shepherd/extensions/workloads/pods" + "github.com/rancher/shepherd/extensions/defaults/namespaces" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" kwait "k8s.io/apimachinery/pkg/util/wait" ) const ( - repoType = "catalog.cattle.io.clusterrepo" - appsType = "catalog.cattle.io.apps" awsUpstreamCloudProviderRepo = "https://github.com/kubernetes/cloud-provider-aws.git" masterBranch = "master" AwsUpstreamChartName = "aws-cloud-controller-manager" - kubeSystemNamespace = "kube-system" ) // InstallAWSOutOfTreeChart installs the CSI chart for aws cloud provider in a given cluster. @@ -37,12 +35,12 @@ func InstallAWSOutOfTreeChart(client *rancher.Client, installOptions *InstallOpt awsChartInstallActionPayload := &payloadOpts{ InstallOptions: *installOptions, Name: AwsUpstreamChartName, - Namespace: kubeSystemNamespace, + Namespace: namespaces.KubeSystem, Host: serverSetting.Value, DefaultRegistry: registrySetting.Value, } - chartInstallAction := awsChartInstallAction(awsChartInstallActionPayload, repoName, kubeSystemNamespace, installOptions.ProjectID) + chartInstallAction := awsChartInstallAction(awsChartInstallActionPayload, repoName, namespaces.KubeSystem, installOptions.ProjectID) catalogClient, err := client.GetClusterCatalogClient(installOptions.ClusterID) if err != nil { @@ -54,7 +52,7 @@ func InstallAWSOutOfTreeChart(client *rancher.Client, installOptions *InstallOpt return err } - err = VerifyChartInstall(catalogClient, kubeSystemNamespace, AwsUpstreamChartName) + err = VerifyChartInstall(catalogClient, namespaces.KubeSystem, AwsUpstreamChartName) if err != nil { return err } @@ -67,7 +65,7 @@ func InstallAWSOutOfTreeChart(client *rancher.Client, installOptions *InstallOpt chartNodeSelector := map[string]string{ "node-role.kubernetes.io/controlplane": "true", } - err = updateHelmNodeSelectors(steveclient, kubeSystemNamespace, AwsUpstreamChartName, chartNodeSelector) + err = updateHelmNodeSelectors(steveclient, namespaces.KubeSystem, AwsUpstreamChartName, chartNodeSelector) return err } @@ -237,7 +235,7 @@ func awsChartInstallAction(awsChartInstallActionPayload *payloadOpts, repoName, // upstream bug in helm charts, where you can't override the nodeSelector during a deployment of an upstream chart. func updateHelmNodeSelectors(client *steveV1.Client, daemonsetNamespace, daemonsetName string, newNodeSelector map[string]string) error { err := kwait.Poll(1*time.Second, 1*time.Minute, func() (done bool, err error) { - _, err = client.SteveType(pods.DaemonsetSteveType).ByID(daemonsetNamespace + "/" + daemonsetName) + _, err = client.SteveType(stevetypes.Daemonset).ByID(daemonsetNamespace + "/" + daemonsetName) if err != nil { return false, nil } @@ -247,7 +245,7 @@ func updateHelmNodeSelectors(client *steveV1.Client, daemonsetNamespace, daemons return err } - steveDaemonset, err := client.SteveType(pods.DaemonsetSteveType).ByID(daemonsetNamespace + "/" + daemonsetName) + steveDaemonset, err := client.SteveType(stevetypes.Daemonset).ByID(daemonsetNamespace + "/" + daemonsetName) if err != nil { return err } @@ -260,6 +258,6 @@ func updateHelmNodeSelectors(client *steveV1.Client, daemonsetNamespace, daemons daemonsetObject.Spec.Template.Spec.NodeSelector = newNodeSelector - _, err = client.SteveType(pods.DaemonsetSteveType).Update(steveDaemonset, daemonsetObject) + _, err = client.SteveType(stevetypes.Daemonset).Update(steveDaemonset, daemonsetObject) return err } diff --git a/extensions/charts/charts.go b/extensions/charts/charts.go index 5fe8bc77..bbe7f2fb 100644 --- a/extensions/charts/charts.go +++ b/extensions/charts/charts.go @@ -7,7 +7,8 @@ import ( catalogv1 "github.com/rancher/rancher/pkg/apis/catalog.cattle.io/v1" "github.com/rancher/shepherd/clients/rancher" steveV1 "github.com/rancher/shepherd/clients/rancher/v1" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" "github.com/rancher/shepherd/extensions/kubeapi/workloads/daemonsets" "github.com/rancher/shepherd/extensions/kubeapi/workloads/deployments" "github.com/rancher/shepherd/pkg/api/scheme" @@ -150,7 +151,7 @@ func WatchAndWaitDeployments(client *rancher.Client, clusterID, namespace string for _, deployment := range deploymentList { watchAppInterface, err := adminDeploymentResource.Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + deployment.Name, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -190,7 +191,7 @@ func WatchAndWaitDeploymentForAnnotation(client *rancher.Client, clusterID, name watchAppInterface, err := adminDeploymentResource.Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + deploymentName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -250,7 +251,7 @@ func WatchAndWaitDaemonSets(client *rancher.Client, clusterID, namespace string, for _, daemonSet := range daemonSetList { watchAppInterface, err := adminDaemonSetResource.Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + daemonSet.Name, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -308,7 +309,7 @@ func WatchAndWaitStatefulSets(client *rancher.Client, clusterID, namespace strin for _, statefulSet := range statefulSetList { watchAppInterface, err := adminStatefulSetResource.Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + statefulSet.Name, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -345,13 +346,13 @@ func CreateChartRepoFromGithub(client *steveV1.Client, githubURL, githubBranch, InsecureSkipTLSverify: true, }, } - _, err := client.SteveType(repoType).Create(repoObject) + _, err := client.SteveType(stevetypes.ClusterRepo).Create(repoObject) if err != nil { return err } err = kwait.Poll(1*time.Second, 2*time.Minute, func() (done bool, err error) { - res, err := client.SteveType(repoType).List(nil) + res, err := client.SteveType(stevetypes.ClusterRepo).List(nil) if err != nil { return false, err } diff --git a/extensions/charts/payloads.go b/extensions/charts/payloads.go index 4bd4b9ad..86980a00 100644 --- a/extensions/charts/payloads.go +++ b/extensions/charts/payloads.go @@ -4,6 +4,7 @@ import ( "time" v3 "github.com/rancher/rancher/pkg/apis/management.cattle.io/v3" + "github.com/rancher/shepherd/extensions/defaults/annotations" "github.com/rancher/shepherd/pkg/api/steve/catalog/types" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -50,8 +51,8 @@ func newChartUpgradeAction(namespace string, chartUpgrades []types.ChartUpgrade) func newChartInstall(name, version, clusterID, clusterName, url, repoName, projectID, defaultRegistry string, chartValues map[string]interface{}) *types.ChartInstall { chartInstall := types.ChartInstall{ Annotations: map[string]string{ - "catalog.cattle.io/ui-source-repo": repoName, - "catalog.cattle.io/ui-source-repo-type": "cluster", + annotations.UiSourceRepo: repoName, + annotations.UiSourceRepoType: "cluster", }, ChartName: name, ReleaseName: name, @@ -83,8 +84,8 @@ func newChartInstall(name, version, clusterID, clusterName, url, repoName, proje func newChartUpgrade(name, version, clusterID, clusterName, url, defaultRegistry string, chartValues map[string]interface{}) *types.ChartUpgrade { chartUpgrade := types.ChartUpgrade{ Annotations: map[string]string{ - "catalog.cattle.io/ui-source-repo": "rancher-charts", - "catalog.cattle.io/ui-source-repo-type": "cluster", + annotations.UiSourceRepo: "rancher-charts", + annotations.UiSourceRepoType: "cluster", }, ChartName: name, ReleaseName: name, diff --git a/extensions/charts/ranchergatekeeper.go b/extensions/charts/ranchergatekeeper.go index 054e6b09..e4a90dff 100644 --- a/extensions/charts/ranchergatekeeper.go +++ b/extensions/charts/ranchergatekeeper.go @@ -7,9 +7,9 @@ import ( catalogv1 "github.com/rancher/rancher/pkg/apis/catalog.cattle.io/v1" "github.com/rancher/shepherd/clients/rancher" "github.com/rancher/shepherd/clients/rancher/catalog" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" kubenamespaces "github.com/rancher/shepherd/extensions/kubeapi/namespaces" - "github.com/rancher/shepherd/extensions/namespaces" "github.com/rancher/shepherd/pkg/api/steve/catalog/types" "github.com/rancher/shepherd/pkg/wait" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -66,7 +66,7 @@ func InstallRancherGatekeeperChart(client *rancher.Client, installOptions *Insta watchAppInterface, err := catalogClient.Apps(RancherGatekeeperNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherGatekeeperName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -94,7 +94,7 @@ func InstallRancherGatekeeperChart(client *rancher.Client, installOptions *Insta watchAppInterface, err = catalogClient.Apps(RancherGatekeeperNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherGatekeeperCRDName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -120,7 +120,7 @@ func InstallRancherGatekeeperChart(client *rancher.Client, installOptions *Insta return err } - namespaceClient := steveclient.SteveType(namespaces.NamespaceSteveType) + namespaceClient := steveclient.SteveType(stevetypes.Namespace) namespace, err := namespaceClient.ByID(RancherGatekeeperNamespace) if err != nil { @@ -144,7 +144,7 @@ func InstallRancherGatekeeperChart(client *rancher.Client, installOptions *Insta watchNamespaceInterface, err := adminNamespaceResource.Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherGatekeeperNamespace, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { @@ -167,7 +167,7 @@ func InstallRancherGatekeeperChart(client *rancher.Client, installOptions *Insta // wait for chart to be fully deployed watchAppInterface, err := catalogClient.Apps(RancherGatekeeperNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherGatekeeperName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -244,7 +244,7 @@ func UpgradeRancherGatekeeperChart(client *rancher.Client, installOptions *Insta // wait for chart to be in status pending upgrade watchAppInterface, err := adminCatalogClient.Apps(RancherGatekeeperNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherGatekeeperName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -266,7 +266,7 @@ func UpgradeRancherGatekeeperChart(client *rancher.Client, installOptions *Insta // wait for chart to be full deployed watchAppInterface, err = adminCatalogClient.Apps(RancherGatekeeperNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherGatekeeperName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err diff --git a/extensions/charts/rancheristio.go b/extensions/charts/rancheristio.go index 470486f8..8d2783ff 100644 --- a/extensions/charts/rancheristio.go +++ b/extensions/charts/rancheristio.go @@ -7,9 +7,9 @@ import ( catalogv1 "github.com/rancher/rancher/pkg/apis/catalog.cattle.io/v1" "github.com/rancher/shepherd/clients/rancher" "github.com/rancher/shepherd/clients/rancher/catalog" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" kubenamespaces "github.com/rancher/shepherd/extensions/kubeapi/namespaces" - "github.com/rancher/shepherd/extensions/namespaces" "github.com/rancher/shepherd/pkg/api/steve/catalog/types" "github.com/rancher/shepherd/pkg/wait" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -62,7 +62,7 @@ func InstallRancherIstioChart(client *rancher.Client, installOptions *InstallOpt watchAppInterface, err := catalogClient.Apps(RancherIstioNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherIstioName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -85,7 +85,7 @@ func InstallRancherIstioChart(client *rancher.Client, installOptions *InstallOpt return err } - namespaceClient := steveclient.SteveType(namespaces.NamespaceSteveType) + namespaceClient := steveclient.SteveType(stevetypes.Namespace) namespace, err := namespaceClient.ByID(RancherIstioNamespace) if err != nil { @@ -109,7 +109,7 @@ func InstallRancherIstioChart(client *rancher.Client, installOptions *InstallOpt watchNamespaceInterface, err := adminNamespaceResource.Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherIstioNamespace, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { @@ -132,7 +132,7 @@ func InstallRancherIstioChart(client *rancher.Client, installOptions *InstallOpt // wait for chart to be full deployed watchAppInterface, err := catalogClient.Apps(RancherIstioNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherIstioName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -230,7 +230,7 @@ func UpgradeRancherIstioChart(client *rancher.Client, installOptions *InstallOpt // wait for chart to be in status pending upgrade watchAppInterface, err := adminCatalogClient.Apps(RancherIstioNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherIstioName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -252,7 +252,7 @@ func UpgradeRancherIstioChart(client *rancher.Client, installOptions *InstallOpt // wait for chart to be full deployed watchAppInterface, err = adminCatalogClient.Apps(RancherIstioNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherIstioName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err diff --git a/extensions/charts/rancherlogging.go b/extensions/charts/rancherlogging.go index 11f1946d..2da19a1b 100644 --- a/extensions/charts/rancherlogging.go +++ b/extensions/charts/rancherlogging.go @@ -7,9 +7,9 @@ import ( catalogv1 "github.com/rancher/rancher/pkg/apis/catalog.cattle.io/v1" "github.com/rancher/shepherd/clients/rancher" "github.com/rancher/shepherd/clients/rancher/catalog" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" kubenamespaces "github.com/rancher/shepherd/extensions/kubeapi/namespaces" - "github.com/rancher/shepherd/extensions/namespaces" "github.com/rancher/shepherd/pkg/api/steve/catalog/types" "github.com/rancher/shepherd/pkg/wait" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -64,7 +64,7 @@ func InstallRancherLoggingChart(client *rancher.Client, installOptions *InstallO watchAppInterface, err := catalogClient.Apps(RancherLoggingNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherLoggingName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -89,7 +89,7 @@ func InstallRancherLoggingChart(client *rancher.Client, installOptions *InstallO watchAppInterface, err = catalogClient.Apps(RancherLoggingNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherLoggingCRDName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -115,7 +115,7 @@ func InstallRancherLoggingChart(client *rancher.Client, installOptions *InstallO return err } - namespaceClient := steveclient.SteveType(namespaces.NamespaceSteveType) + namespaceClient := steveclient.SteveType(stevetypes.Namespace) namespace, err := namespaceClient.ByID(RancherLoggingNamespace) if err != nil { @@ -139,7 +139,7 @@ func InstallRancherLoggingChart(client *rancher.Client, installOptions *InstallO watchNamespaceInterface, err := adminNamespaceResource.Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherLoggingNamespace, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { @@ -162,7 +162,7 @@ func InstallRancherLoggingChart(client *rancher.Client, installOptions *InstallO // wait for chart to be full deployed watchAppInterface, err := catalogClient.Apps(RancherLoggingNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherLoggingName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err diff --git a/extensions/charts/ranchermonitoring.go b/extensions/charts/ranchermonitoring.go index 5e6f0c00..5a4cc89b 100644 --- a/extensions/charts/ranchermonitoring.go +++ b/extensions/charts/ranchermonitoring.go @@ -7,9 +7,9 @@ import ( catalogv1 "github.com/rancher/rancher/pkg/apis/catalog.cattle.io/v1" "github.com/rancher/shepherd/clients/rancher" "github.com/rancher/shepherd/clients/rancher/catalog" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" kubenamespaces "github.com/rancher/shepherd/extensions/kubeapi/namespaces" - "github.com/rancher/shepherd/extensions/namespaces" "github.com/rancher/shepherd/pkg/api/steve/catalog/types" "github.com/rancher/shepherd/pkg/wait" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -66,7 +66,7 @@ func InstallRancherMonitoringChart(client *rancher.Client, installOptions *Insta watchAppInterface, err := catalogClient.Apps(RancherMonitoringNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherMonitoringName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -91,7 +91,7 @@ func InstallRancherMonitoringChart(client *rancher.Client, installOptions *Insta watchAppInterface, err = catalogClient.Apps(RancherMonitoringNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherMonitoringCRDName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -117,7 +117,7 @@ func InstallRancherMonitoringChart(client *rancher.Client, installOptions *Insta return err } - namespaceClient := steveclient.SteveType(namespaces.NamespaceSteveType) + namespaceClient := steveclient.SteveType(stevetypes.Namespace) namespace, err := namespaceClient.ByID(RancherMonitoringNamespace) if err != nil { @@ -141,7 +141,7 @@ func InstallRancherMonitoringChart(client *rancher.Client, installOptions *Insta watchNamespaceInterface, err := adminNamespaceResource.Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherMonitoringNamespace, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { @@ -164,7 +164,7 @@ func InstallRancherMonitoringChart(client *rancher.Client, installOptions *Insta // wait for chart to be full deployed watchAppInterface, err := catalogClient.Apps(RancherMonitoringNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherMonitoringName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -265,7 +265,7 @@ func UpgradeRancherMonitoringChart(client *rancher.Client, installOptions *Insta // wait for chart to be in status pending upgrade watchAppInterface, err := adminCatalogClient.Apps(RancherMonitoringNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherMonitoringName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -287,7 +287,7 @@ func UpgradeRancherMonitoringChart(client *rancher.Client, installOptions *Insta // wait for chart to be full deployed watchAppInterface, err = adminCatalogClient.Apps(RancherMonitoringNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + RancherMonitoringName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err diff --git a/extensions/charts/verify.go b/extensions/charts/verify.go index d5129d68..bac9e50c 100644 --- a/extensions/charts/verify.go +++ b/extensions/charts/verify.go @@ -5,7 +5,7 @@ import ( v1 "github.com/rancher/rancher/pkg/apis/catalog.cattle.io/v1" "github.com/rancher/shepherd/clients/rancher/catalog" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/timeouts" "github.com/rancher/shepherd/pkg/wait" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/watch" @@ -15,7 +15,7 @@ import ( func VerifyChartInstall(client *catalog.Client, chartNamespace, chartName string) error { watchAppInterface, err := client.Apps(chartNamespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + chartName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err diff --git a/extensions/clusters/bundledclusters/get.go b/extensions/clusters/bundledclusters/get.go index 59957e1e..9409ad4f 100644 --- a/extensions/clusters/bundledclusters/get.go +++ b/extensions/clusters/bundledclusters/get.go @@ -2,7 +2,7 @@ package bundledclusters import ( "github.com/rancher/shepherd/clients/rancher" - "github.com/rancher/shepherd/extensions/clusters" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" ) // Get is a method of BundledCluster that uses provisioning and management clients @@ -11,7 +11,7 @@ func (bc *BundledCluster) Get(client *rancher.Client) (cluster *BundledCluster, cluster = new(BundledCluster) cluster.Meta = bc.Meta - steveclient := client.Steve.SteveType(clusters.ProvisioningSteveResourceType) + steveclient := client.Steve.SteveType(stevetypes.Provisioning) if err != nil { return } diff --git a/extensions/clusters/bundledclusters/update.go b/extensions/clusters/bundledclusters/update.go index ede91484..8b9f0d57 100644 --- a/extensions/clusters/bundledclusters/update.go +++ b/extensions/clusters/bundledclusters/update.go @@ -9,6 +9,7 @@ import ( v1 "github.com/rancher/shepherd/clients/rancher/v1" "github.com/rancher/shepherd/clients/rkecli" "github.com/rancher/shepherd/extensions/clusters" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" ) // Update is a method of BundledCluster that uses provisioning and management clients @@ -17,7 +18,7 @@ func (bc *BundledCluster) Update(client *rancher.Client, cUpdates *BundledCluste updatedCluster = new(BundledCluster) updatedCluster.Meta = bc.Meta - steveclient := client.Steve.SteveType(clusters.ProvisioningSteveResourceType) + steveclient := client.Steve.SteveType(stevetypes.Provisioning) if err != nil { return } diff --git a/extensions/clusters/clusters.go b/extensions/clusters/clusters.go index 860aa175..7836c386 100644 --- a/extensions/clusters/clusters.go +++ b/extensions/clusters/clusters.go @@ -14,9 +14,10 @@ import ( "github.com/rancher/shepherd/clients/rancher" management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" v1 "github.com/rancher/shepherd/clients/rancher/v1" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/annotations" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" "github.com/rancher/shepherd/extensions/provisioninginput" - "github.com/rancher/shepherd/extensions/workloads/pods" "github.com/rancher/shepherd/pkg/api/scheme" "github.com/rancher/shepherd/pkg/wait" "github.com/rancher/wrangler/pkg/summary" @@ -45,7 +46,6 @@ const ( kubeletArgKey = "kubelet-arg" kubeletAPIServerArgKey = "kubeapi-server-arg" kubeControllerManagerArgKey = "kube-controller-manager-arg" - cloudProviderAnnotationName = "cloud-provider-name" disableCloudController = "disable-cloud-controller" protectKernelDefaults = "protect-kernel-defaults" localcluster = "fleet-local/local" @@ -58,7 +58,7 @@ const ( // GetV1ProvisioningClusterByName is a helper function that returns the cluster ID by name func GetV1ProvisioningClusterByName(client *rancher.Client, clusterName string) (string, error) { - clusterList, err := client.Steve.SteveType(ProvisioningSteveResourceType).List(nil) + clusterList, err := client.Steve.SteveType(stevetypes.Provisioning).List(nil) if err != nil { return "", err } @@ -162,7 +162,7 @@ func CheckServiceAccountTokenSecret(client *rancher.Client, clusterName string) // CreateRancherBaselinePSACT creates custom PSACT called rancher-baseline which sets each PSS to baseline. func CreateRancherBaselinePSACT(client *rancher.Client, psact string) error { - _, err := client.Steve.SteveType(PodSecurityAdmissionSteveResoureType).ByID(psact) + _, err := client.Steve.SteveType(stevetypes.PodSecurityAdmission).ByID(psact) if err == nil { return err } @@ -209,7 +209,7 @@ func CreateRancherBaselinePSACT(client *rancher.Client, psact string) error { }, } - _, err = client.Steve.SteveType(PodSecurityAdmissionSteveResoureType).Create(template) + _, err = client.Steve.SteveType(stevetypes.PodSecurityAdmission).Create(template) if err != nil { return err } @@ -488,8 +488,8 @@ func awsOutOfTreeSystemConfig() (rkeConfig []rkev1.RKESystemConfig) { } configData := map[string]interface{}{ - cloudProviderAnnotationName: provisioninginput.AWSProviderName, - protectKernelDefaults: false, + annotations.CloudProviderName: provisioninginput.AWSProviderName, + protectKernelDefaults: false, } rkeConfig = append(rkeConfig, RKESystemConfigTemplate(configData, nil)) @@ -921,7 +921,7 @@ func CreateRKE1Cluster(client *rancher.Client, rke1Cluster *management.Cluster) watchInterface, err := adminClient.GetManagementWatchInterface(management.ClusterType, metav1.ListOptions{ FieldSelector: "metadata.name=" + clusterResp.ID, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -943,7 +943,7 @@ func CreateRKE1Cluster(client *rancher.Client, rke1Cluster *management.Cluster) // CreateK3SRKE2Cluster is a "helper" functions that takes a rancher client, and the rke2 cluster config as parameters. This function // registers a delete cluster fuction with a wait.WatchWait to ensure the cluster is removed cleanly. func CreateK3SRKE2Cluster(client *rancher.Client, rke2Cluster *apisV1.Cluster) (*v1.SteveAPIObject, error) { - cluster, err := client.Steve.SteveType(ProvisioningSteveResourceType).Create(rke2Cluster) + cluster, err := client.Steve.SteveType(stevetypes.Provisioning).Create(rke2Cluster) if err != nil { return nil, err } @@ -954,7 +954,7 @@ func CreateK3SRKE2Cluster(client *rancher.Client, rke2Cluster *apisV1.Cluster) ( return false, err } - _, err = client.Steve.SteveType(ProvisioningSteveResourceType).ByID(cluster.ID) + _, err = client.Steve.SteveType(stevetypes.Provisioning).ByID(cluster.ID) if err != nil { return false, nil } @@ -979,7 +979,7 @@ func CreateK3SRKE2Cluster(client *rancher.Client, rke2Cluster *apisV1.Cluster) ( watchInterface, err := provKubeClient.Clusters(cluster.ObjectMeta.Namespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + cluster.ObjectMeta.Name, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { @@ -991,7 +991,7 @@ func CreateK3SRKE2Cluster(client *rancher.Client, rke2Cluster *apisV1.Cluster) ( return err } - err = client.Steve.SteveType(ProvisioningSteveResourceType).Delete(cluster) + err = client.Steve.SteveType(stevetypes.Provisioning).Delete(cluster) if err != nil { return err } @@ -1032,13 +1032,13 @@ func DeleteRKE1Cluster(client *rancher.Client, clusterID string) error { // DeleteK3SRKE2Cluster is a "helper" functions that takes a rancher client, and the non-rke1 cluster ID as parameters to delete // the cluster. func DeleteK3SRKE2Cluster(client *rancher.Client, clusterID string) error { - cluster, err := client.Steve.SteveType(ProvisioningSteveResourceType).ByID(clusterID) + cluster, err := client.Steve.SteveType(stevetypes.Provisioning).ByID(clusterID) if err != nil { return err } logrus.Infof("Deleting cluster %s...", cluster.Name) - err = client.Steve.SteveType(ProvisioningSteveResourceType).Delete(cluster) + err = client.Steve.SteveType(stevetypes.Provisioning).Delete(cluster) if err != nil { return err } @@ -1048,14 +1048,14 @@ func DeleteK3SRKE2Cluster(client *rancher.Client, clusterID string) error { // UpdateK3SRKE2Cluster is a "helper" functions that takes a rancher client, old rke2/k3s cluster config, and the new rke2/k3s cluster config as parameters. func UpdateK3SRKE2Cluster(client *rancher.Client, cluster *v1.SteveAPIObject, updatedCluster *apisV1.Cluster) (*v1.SteveAPIObject, error) { - updateCluster, err := client.Steve.SteveType(ProvisioningSteveResourceType).ByID(cluster.ID) + updateCluster, err := client.Steve.SteveType(stevetypes.Provisioning).ByID(cluster.ID) if err != nil { return nil, err } updatedCluster.ObjectMeta.ResourceVersion = updateCluster.ObjectMeta.ResourceVersion - cluster, err = client.Steve.SteveType(ProvisioningSteveResourceType).Update(cluster, updatedCluster) + cluster, err = client.Steve.SteveType(stevetypes.Provisioning).Update(cluster, updatedCluster) if err != nil { return nil, err } @@ -1066,7 +1066,7 @@ func UpdateK3SRKE2Cluster(client *rancher.Client, cluster *v1.SteveAPIObject, up return false, err } - clusterResp, err := client.Steve.SteveType(ProvisioningSteveResourceType).ByID(cluster.ID) + clusterResp, err := client.Steve.SteveType(stevetypes.Provisioning).ByID(cluster.ID) if err != nil { return false, err } @@ -1083,7 +1083,7 @@ func UpdateK3SRKE2Cluster(client *rancher.Client, cluster *v1.SteveAPIObject, up return false, err } - _, err = proxyClient.SteveType(pods.PodResourceSteveType).List(nil) + _, err = proxyClient.SteveType(stevetypes.Pod).List(nil) if err != nil { return false, nil } @@ -1108,7 +1108,7 @@ func WaitClusterToBeInUpgrade(client *rancher.Client, clusterID string) (err err var clusterInfo string opts := metav1.ListOptions{ FieldSelector: "metadata.name=" + clusterID, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), } watchInterface, err := client.GetManagementWatchInterface(management.ClusterType, opts) @@ -1147,7 +1147,7 @@ func WaitClusterUntilUpgrade(client *rancher.Client, clusterID string) (err erro var clusterInfo string opts := metav1.ListOptions{ FieldSelector: "metadata.name=" + clusterID, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), } watchInterfaceWaitUpgrade, err := client.GetManagementWatchInterface(management.ClusterType, opts) @@ -1227,7 +1227,7 @@ func logClusterInfoWithChanges(clusterID, clusterInfo string, summary summary.Su func WatchAndWaitForCluster(client *rancher.Client, steveID string) error { var clusterResp *v1.SteveAPIObject err := kwait.Poll(500*time.Millisecond, 2*time.Minute, func() (done bool, err error) { - clusterResp, err = client.Steve.SteveType(ProvisioningSteveResourceType).ByID(steveID) + clusterResp, err = client.Steve.SteveType(stevetypes.Provisioning).ByID(steveID) if err != nil { return false, err } @@ -1250,7 +1250,7 @@ func WatchAndWaitForCluster(client *rancher.Client, steveID string) error { result, err := kubeProvisioningClient.Clusters(clusterResp.ObjectMeta.Namespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + clusterResp.Name, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return err @@ -1262,7 +1262,7 @@ func WatchAndWaitForCluster(client *rancher.Client, steveID string) error { // GetProvisioningClusterByName is a helper function to get cluster object with the cluster name func GetProvisioningClusterByName(client *rancher.Client, clusterName string, namespace string) (*apisV1.Cluster, *v1.SteveAPIObject, error) { - clusterObj, err := client.Steve.SteveType(ProvisioningSteveResourceType).ByID(namespace + "/" + clusterName) + clusterObj, err := client.Steve.SteveType(stevetypes.Provisioning).ByID(namespace + "/" + clusterName) if err != nil { return nil, nil, err } @@ -1301,7 +1301,7 @@ func WaitForActiveRKE1Cluster(client *rancher.Client, clusterID string) error { // ListDownstreamClusters is a helper function to get the name of the downstream clusters func ListDownstreamClusters(client *rancher.Client) (clusterNames []string, err error) { - clusterList, err := client.Steve.SteveType(ProvisioningSteveResourceType).ListAll(nil) + clusterList, err := client.Steve.SteveType(stevetypes.Provisioning).ListAll(nil) if err != nil { return nil, errors.Wrap(err, ErrMsgListDownstreamClusters) } diff --git a/extensions/clusters/import.go b/extensions/clusters/import.go index db5dcd89..9c91dd03 100644 --- a/extensions/clusters/import.go +++ b/extensions/clusters/import.go @@ -10,6 +10,7 @@ import ( "github.com/rancher/shepherd/clients/dynamic" "github.com/rancher/shepherd/clients/rancher" management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" + "github.com/rancher/shepherd/extensions/defaults/namespaces" ext_unstructured "github.com/rancher/shepherd/extensions/unstructured" "github.com/rancher/shepherd/pkg/wait" batchv1 "k8s.io/api/batch/v1" @@ -92,7 +93,7 @@ func ImportCluster(client *rancher.Client, cluster *apisV1.Cluster, rest *rest.C Name: "rancher-installer", }, } - _, err = downClient.Resource(corev1.SchemeGroupVersion.WithResource("serviceaccounts")).Namespace("kube-system").Create(context.TODO(), ext_unstructured.MustToUnstructured(sa), metav1.CreateOptions{}) + _, err = downClient.Resource(corev1.SchemeGroupVersion.WithResource("serviceaccounts")).Namespace(namespaces.KubeSystem).Create(context.TODO(), ext_unstructured.MustToUnstructured(sa), metav1.CreateOptions{}) if err != nil { return err } @@ -105,7 +106,7 @@ func ImportCluster(client *rancher.Client, cluster *apisV1.Cluster, rest *rest.C { Kind: "ServiceAccount", Name: sa.Name, - Namespace: "kube-system", + Namespace: namespaces.KubeSystem, }, }, RoleRef: rbacv1.RoleRef{ @@ -127,7 +128,7 @@ func ImportCluster(client *rancher.Client, cluster *apisV1.Cluster, rest *rest.C "config": kubeConfig, }, } - _, err = downClient.Resource(corev1.SchemeGroupVersion.WithResource("configmaps")).Namespace("kube-system").Create(context.TODO(), ext_unstructured.MustToUnstructured(cm), metav1.CreateOptions{}) + _, err = downClient.Resource(corev1.SchemeGroupVersion.WithResource("configmaps")).Namespace(namespaces.KubeSystem).Create(context.TODO(), ext_unstructured.MustToUnstructured(cm), metav1.CreateOptions{}) if err != nil { return err } @@ -182,12 +183,12 @@ func ImportCluster(client *rancher.Client, cluster *apisV1.Cluster, rest *rest.C }, }, } - _, err = downClient.Resource(batchv1.SchemeGroupVersion.WithResource("jobs")).Namespace("kube-system").Create(context.TODO(), ext_unstructured.MustToUnstructured(job), metav1.CreateOptions{}) + _, err = downClient.Resource(batchv1.SchemeGroupVersion.WithResource("jobs")).Namespace(namespaces.KubeSystem).Create(context.TODO(), ext_unstructured.MustToUnstructured(job), metav1.CreateOptions{}) if err != nil { return err } - jobWatch, err := downClient.Resource(batchv1.SchemeGroupVersion.WithResource("jobs")).Namespace("kube-system").Watch(context.TODO(), metav1.ListOptions{ + jobWatch, err := downClient.Resource(batchv1.SchemeGroupVersion.WithResource("jobs")).Namespace(namespaces.KubeSystem).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: fields.OneTermEqualSelector("metadata.name", job.Name).String(), TimeoutSeconds: &importTimeout, }) diff --git a/extensions/codecoverage/codecoverage.go b/extensions/codecoverage/codecoverage.go index 084d6019..a126bfeb 100644 --- a/extensions/codecoverage/codecoverage.go +++ b/extensions/codecoverage/codecoverage.go @@ -9,7 +9,8 @@ import ( apiv1 "github.com/rancher/rancher/pkg/apis/provisioning.cattle.io/v1" "github.com/rancher/shepherd/clients/rancher" v1 "github.com/rancher/shepherd/clients/rancher/v1" - "github.com/rancher/shepherd/extensions/clusters" + "github.com/rancher/shepherd/extensions/defaults/namespaces" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" "github.com/rancher/shepherd/extensions/kubeconfig" "github.com/rancher/shepherd/pkg/killserver" "github.com/sirupsen/logrus" @@ -23,16 +24,15 @@ import ( var podGroupVersionResource = corev1.SchemeGroupVersion.WithResource("pods") const ( - cattleSystemNameSpace = "cattle-system" - localCluster = "local" - rancherCoverFile = "ranchercoverage" - agentCoverFile = "agentcoverage" - outputDir = "cover" + localCluster = "local" + rancherCoverFile = "ranchercoverage" + agentCoverFile = "agentcoverage" + outputDir = "cover" ) func checkServiceIsRunning(dynamicClient dynamic.Interface) error { return kwait.Poll(500*time.Millisecond, 2*time.Minute, func() (done bool, err error) { - _, err = dynamicClient.Resource(podGroupVersionResource).Namespace(cattleSystemNameSpace).List(context.Background(), metav1.ListOptions{}) + _, err = dynamicClient.Resource(podGroupVersionResource).Namespace(namespaces.CattleSystem).List(context.Background(), metav1.ListOptions{}) if k8sErrors.IsInternalError(err) || k8sErrors.IsServiceUnavailable(err) { return false, nil } else if err != nil { @@ -60,7 +60,7 @@ func killTestServices(client *rancher.Client, clusterID string, podNames []strin } for _, podName := range podNames { - _, err := kubeconfig.KubectlExec(restConfig, podName, cattleSystemNameSpace, cmd) + _, err := kubeconfig.KubectlExec(restConfig, podName, namespaces.CattleSystem, cmd) if err != nil { logrus.Errorf("error killing pod container %v", err) } @@ -84,7 +84,7 @@ func retrieveCodeCoverageFile(client *rancher.Client, clusterID, coverageFilenam fileName := fmt.Sprintf("%s%s", podName, coverageFilename) dst := fmt.Sprintf("%s/%s", outputDir, fileName) - err := kubeconfig.CopyFileFromPod(restConfig, *kubeConfig, podName, cattleSystemNameSpace, coverageFilename, dst) + err := kubeconfig.CopyFileFromPod(restConfig, *kubeConfig, podName, namespaces.CattleSystem, coverageFilename, dst) if err != nil { return err } @@ -103,7 +103,7 @@ func KillRancherTestServicesRetrieveCoverage(client *rancher.Client) error { return err } - pods, err := dynamicClient.Resource(podGroupVersionResource).Namespace(cattleSystemNameSpace).List(context.Background(), metav1.ListOptions{}) + pods, err := dynamicClient.Resource(podGroupVersionResource).Namespace(namespaces.CattleSystem).List(context.Background(), metav1.ListOptions{}) if err != nil { return err } @@ -132,7 +132,7 @@ func KillRancherTestServicesRetrieveCoverage(client *rancher.Client) error { // inorder for the code coverage report to be written, and then copies over the coverage reports from the pods // to a local destination. The custom code coverage rancher-agent image must be running in the downstream cluster. func KillAgentTestServicesRetrieveCoverage(client *rancher.Client) error { - clusters, err := client.Steve.SteveType(clusters.ProvisioningSteveResourceType).ListAll(nil) + clusters, err := client.Steve.SteveType(stevetypes.Provisioning).ListAll(nil) if err != nil { return err } @@ -151,7 +151,7 @@ func KillAgentTestServicesRetrieveCoverage(client *rancher.Client) error { continue } - pods, err := dynamicClient.Resource(podGroupVersionResource).Namespace(cattleSystemNameSpace).List(context.Background(), metav1.ListOptions{}) + pods, err := dynamicClient.Resource(podGroupVersionResource).Namespace(namespaces.CattleSystem).List(context.Background(), metav1.ListOptions{}) if err != nil { logrus.Errorf("could not list pods") continue diff --git a/extensions/defaults/annotations/annotations.go b/extensions/defaults/annotations/annotations.go new file mode 100644 index 00000000..53dea733 --- /dev/null +++ b/extensions/defaults/annotations/annotations.go @@ -0,0 +1,12 @@ +package annotations + +const ( + Machine = "cluster.x-k8s.io/machine" + ExternalIp = "rke.cattle.io/external-ip" + ControlPlaneLeader = "control-plane.alpha.kubernetes.io/leader" + CloudProviderName = "cloud-provider-name" + UiSourceRepo = "catalog.cattle.io/ui-source-repo" + UiSourceRepoType = "catalog.cattle.io/ui-source-repo-type" + ContainerResourceLimit = "field.cattle.io/containerDefaultResourceLimit" + ProjectId = "field.cattle.io/projectId" +) diff --git a/extensions/defaults/labels/labels.go b/extensions/defaults/labels/labels.go new file mode 100644 index 00000000..08682869 --- /dev/null +++ b/extensions/defaults/labels/labels.go @@ -0,0 +1,8 @@ +package labels + +const ( + EtcdRole = "node-role.kubernetes.io/etcd" + ControlplaneRole = "node-role.kubernetes.io/control-plane" + WorkerRole = "node-role.kubernetes.io/worker" + WorkloadSelector = "workload.user.cattle.io/workloadselector" +) diff --git a/extensions/defaults/namespaces/namespaces.go b/extensions/defaults/namespaces/namespaces.go new file mode 100644 index 00000000..a4adc2d8 --- /dev/null +++ b/extensions/defaults/namespaces/namespaces.go @@ -0,0 +1,7 @@ +package namespaces + +const ( + KubeSystem = "kube-system" + CattleSystem = "cattle-system" + Fleet = "fleet-default" +) diff --git a/extensions/defaults/stevetypes/stevetypes.go b/extensions/defaults/stevetypes/stevetypes.go index cb2b6bb2..bf2a4ac5 100644 --- a/extensions/defaults/stevetypes/stevetypes.go +++ b/extensions/defaults/stevetypes/stevetypes.go @@ -1,7 +1,24 @@ package stevetypes const ( - Provisioning = "provisioning.cattle.io.cluster" - EtcdSnapshot = "rke.cattle.io.etcdsnapshot" - FleetCluster = "fleet.cattle.io.cluster" + Provisioning = "provisioning.cattle.io.cluster" + EtcdSnapshot = "rke.cattle.io.etcdsnapshot" + FleetCluster = "fleet.cattle.io.cluster" + ClusterRoleBinding = "rbac.authorization.k8s.io.clusterrolebinding" + PodSecurityAdmission = "management.cattle.io.podsecurityadmissionconfigurationtemplate" + GlobalRoleBinding = "management.cattle.io.globalrolebinding" + ManagementSetting = "management.cattle.io.setting" + CoordinationLease = "coordination.k8s.io.lease" + ClusterRepo = "catalog.cattle.io.clusterrepo" + Apps = "catalog.cattle.io.apps" + Machine = "cluster.x-k8s.io.machine" + Ingress = "networking.k8s.io.ingress" + Deployment = "apps.deployment" + Daemonset = "apps.daemonset" + Service = "service" + ServiceAccount = "serviceaccount" + Node = "node" + Pod = "pod" + Namespace = "namespace" + Configmap = "configmap" ) diff --git a/extensions/defaults/timeouts/timeouts.go b/extensions/defaults/timeouts/timeouts.go new file mode 100644 index 00000000..d590f4a1 --- /dev/null +++ b/extensions/defaults/timeouts/timeouts.go @@ -0,0 +1,21 @@ +package timeouts + +import "time" + +func WatchTimeoutSeconds() *int64 { + timeout := watchTimeoutSeconds + return &timeout +} + +const ( + watchTimeoutSeconds = int64(60 * 30) + FiveHundredMillisecondTimeout = 500 * time.Millisecond + FiveSecondTimeout = 5 * time.Second + TenSecondTimeout = 10 * time.Second + OneMinuteTimeout = 1 * time.Minute + TwoMinuteTimeout = 2 * time.Minute + FiveMinuteTimeout = 5 * time.Minute + TenMinuteTimeout = 10 * time.Minute + FifteenMinuteTimeout = 15 * time.Minute + ThirtyMinuteTimeout = 30 * time.Minute +) diff --git a/extensions/etcdsnapshot/etcdsnapshot.go b/extensions/etcdsnapshot/etcdsnapshot.go index 18a6a2ed..c377e49e 100644 --- a/extensions/etcdsnapshot/etcdsnapshot.go +++ b/extensions/etcdsnapshot/etcdsnapshot.go @@ -14,8 +14,9 @@ import ( management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" rancherv1 "github.com/rancher/shepherd/clients/rancher/v1" "github.com/rancher/shepherd/extensions/clusters" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/namespaces" "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" "github.com/rancher/shepherd/extensions/kubeapi/nodes" "github.com/sirupsen/logrus" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -24,7 +25,6 @@ import ( const ( ProvisioningSteveResouceType = "provisioning.cattle.io.cluster" - fleetNamespace = "fleet-default" localClusterName = "local" active = "active" ) @@ -118,7 +118,7 @@ func CreateRKE1Snapshot(client *rancher.Client, clusterName string) error { return err } - err = wait.Poll(1*time.Second, defaults.FiveMinuteTimeout, func() (bool, error) { + err = wait.Poll(1*time.Second, timeouts.FiveMinuteTimeout, func() (bool, error) { snapshotSteveObjList, err := client.Management.EtcdBackup.ListAll(&types.ListOpts{ Filters: map[string]interface{}{ "clusterId": clusterID, @@ -151,7 +151,7 @@ func CreateRKE1Snapshot(client *rancher.Client, clusterName string) error { // CreateRKE2K3SSnapshot is a helper function to create a snapshot on an RKE2 or k3s cluster. Returns error if any. func CreateRKE2K3SSnapshot(client *rancher.Client, clusterName string) error { - clusterObject, clusterSteveObject, err := clusters.GetProvisioningClusterByName(client, clusterName, fleetNamespace) + clusterObject, clusterSteveObject, err := clusters.GetProvisioningClusterByName(client, clusterName, namespaces.Fleet) if err != nil { return err } @@ -175,24 +175,24 @@ func CreateRKE2K3SSnapshot(client *rancher.Client, clusterName string) error { } logrus.Infof("Creating snapshot...") - _, err = client.Steve.SteveType(clusters.ProvisioningSteveResourceType).Update(clusterSteveObject, clusterObject) + _, err = client.Steve.SteveType(stevetypes.Provisioning).Update(clusterSteveObject, clusterObject) if err != nil { return err } - err = wait.Poll(1*time.Second, defaults.FiveMinuteTimeout, func() (bool, error) { - snapshotSteveObjList, err := client.Steve.SteveType("rke.cattle.io.etcdsnapshot").List(nil) + err = wait.Poll(1*time.Second, timeouts.FiveMinuteTimeout, func() (bool, error) { + snapshotSteveObjList, err := client.Steve.SteveType(stevetypes.EtcdSnapshot).List(nil) if err != nil { return false, nil } - _, clusterSteveObject, err := clusters.GetProvisioningClusterByName(client, clusterName, fleetNamespace) + _, clusterSteveObject, err := clusters.GetProvisioningClusterByName(client, clusterName, namespaces.Fleet) if err != nil { return false, nil } for _, snapshot := range snapshotSteveObjList.Data { - snapshotObj, err := client.Steve.SteveType("rke.cattle.io.etcdsnapshot").ByID(snapshot.ID) + snapshotObj, err := client.Steve.SteveType(stevetypes.EtcdSnapshot).ByID(snapshot.ID) if err != nil { return false, nil } @@ -240,7 +240,7 @@ func RestoreRKE1Snapshot(client *rancher.Client, clusterName string, snapshotRes return err } - err = wait.PollUntilContextTimeout(context.TODO(), 5*time.Second, defaults.ThirtyMinuteTimeout, true, func(ctx context.Context) (done bool, err error) { + err = wait.PollUntilContextTimeout(context.TODO(), 5*time.Second, timeouts.ThirtyMinuteTimeout, true, func(ctx context.Context) (done bool, err error) { clusterResp, err := client.Management.Cluster.ByID(clusterID) if err != nil { return false, nil @@ -261,7 +261,7 @@ func RestoreRKE1Snapshot(client *rancher.Client, clusterName string, snapshotRes // RestoreRKE2K3SSnapshot is a helper function to restore a snapshot on an RKE2 or k3s cluster. Returns error if any. func RestoreRKE2K3SSnapshot(client *rancher.Client, clusterName string, snapshotRestore *rkev1.ETCDSnapshotRestore, initialControlPlaneValue, initialWorkerValue string) error { - clusterObject, existingSteveAPIObject, err := clusters.GetProvisioningClusterByName(client, clusterName, fleetNamespace) + clusterObject, existingSteveAPIObject, err := clusters.GetProvisioningClusterByName(client, clusterName, namespaces.Fleet) if err != nil { return err } @@ -271,7 +271,7 @@ func RestoreRKE2K3SSnapshot(client *rancher.Client, clusterName string, snapshot clusterObject.Spec.RKEConfig.UpgradeStrategy.WorkerConcurrency = initialWorkerValue logrus.Infof("Restoring snapshot: %v", snapshotRestore.Name) - _, err = client.Steve.SteveType(ProvisioningSteveResouceType).Update(existingSteveAPIObject, clusterObject) + _, err = client.Steve.SteveType(stevetypes.Provisioning).Update(existingSteveAPIObject, clusterObject) if err != nil { return err } diff --git a/extensions/ingresses/ingresses.go b/extensions/ingresses/ingresses.go index 9714f50b..51a18c1f 100644 --- a/extensions/ingresses/ingresses.go +++ b/extensions/ingresses/ingresses.go @@ -11,7 +11,8 @@ import ( "github.com/pkg/errors" "github.com/rancher/shepherd/clients/rancher" v1 "github.com/rancher/shepherd/clients/rancher/v1" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" "github.com/rancher/shepherd/extensions/workloads/pods" "github.com/sirupsen/logrus" networking "k8s.io/api/networking/v1" @@ -20,7 +21,6 @@ import ( const ( IngressSteveType = "networking.k8s.io.ingress" - pod = "pod" IngressNginx = "ingress-nginx" RancherWebhook = "rancher-webhook" ) @@ -76,8 +76,8 @@ func IsIngressExternallyAccessible(client *rancher.Client, hostname string, path // CreateIngress will create an Ingress object in the downstream cluster. func CreateIngress(client *v1.Client, ingressName string, ingressTemplate networking.Ingress) (*v1.SteveAPIObject, error) { - podClient := client.SteveType(pod) - err := kwait.PollUntilContextTimeout(context.TODO(), 15*time.Second, defaults.FiveMinuteTimeout, true, func(context.Context) (done bool, err error) { + podClient := client.SteveType(stevetypes.Pod) + err := kwait.PollUntilContextTimeout(context.TODO(), 15*time.Second, timeouts.FiveMinuteTimeout, true, func(context.Context) (done bool, err error) { newPods, err := podClient.List(nil) if err != nil { return false, nil diff --git a/extensions/kubeapi/namespaces/create.go b/extensions/kubeapi/namespaces/create.go index 3003cf89..db95857a 100644 --- a/extensions/kubeapi/namespaces/create.go +++ b/extensions/kubeapi/namespaces/create.go @@ -7,7 +7,8 @@ import ( "github.com/rancher/shepherd/clients/rancher" management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" - "github.com/rancher/shepherd/extensions/defaults" + defaultAnnotations "github.com/rancher/shepherd/extensions/defaults/annotations" + "github.com/rancher/shepherd/extensions/defaults/timeouts" "github.com/rancher/shepherd/extensions/unstructured" "github.com/rancher/shepherd/pkg/api/scheme" "github.com/rancher/shepherd/pkg/wait" @@ -27,10 +28,10 @@ func CreateNamespace(client *rancher.Client, namespaceName, containerDefaultReso annotations = make(map[string]string) } if containerDefaultResourceLimit != "" { - annotations["field.cattle.io/containerDefaultResourceLimit"] = containerDefaultResourceLimit + annotations[defaultAnnotations.ContainerResourceLimit] = containerDefaultResourceLimit } if project != nil { - annotations["field.cattle.io/projectId"] = project.ID + annotations[defaultAnnotations.ProjectId] = project.ID } namespace := &coreV1.Namespace{ ObjectMeta: metav1.ObjectMeta{ @@ -67,7 +68,7 @@ func CreateNamespace(client *rancher.Client, namespaceName, containerDefaultReso clusterRoleWatch, err := clusterRoleResource.Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + fmt.Sprintf("%s-namespaces-edit", projectID), - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { @@ -108,7 +109,7 @@ func CreateNamespace(client *rancher.Client, namespaceName, containerDefaultReso adminNamespaceResource := adminDynamicClient.Resource(NamespaceGroupVersionResource).Namespace("") watchInterface, err := adminNamespaceResource.Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + unstructuredResp.GetName(), - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { diff --git a/extensions/kubeapi/projects/delete.go b/extensions/kubeapi/projects/delete.go index 13a704eb..cb2e54d1 100644 --- a/extensions/kubeapi/projects/delete.go +++ b/extensions/kubeapi/projects/delete.go @@ -4,7 +4,7 @@ import ( "context" "github.com/rancher/shepherd/clients/rancher" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/timeouts" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" kwait "k8s.io/apimachinery/pkg/util/wait" ) @@ -23,7 +23,7 @@ func DeleteProject(client *rancher.Client, projectNamespace string, projectName return err } - err = kwait.Poll(defaults.FiveHundredMillisecondTimeout, defaults.TenSecondTimeout, func() (done bool, err error) { + err = kwait.Poll(timeouts.FiveHundredMillisecondTimeout, timeouts.TenSecondTimeout, func() (done bool, err error) { projectList, err := ListProjects(client, projectNamespace, metav1.ListOptions{ FieldSelector: "metadata.name=" + projectName, }) diff --git a/extensions/kubeapi/workloads/cronjobs/create.go b/extensions/kubeapi/workloads/cronjobs/create.go index d02606bd..041398f4 100644 --- a/extensions/kubeapi/workloads/cronjobs/create.go +++ b/extensions/kubeapi/workloads/cronjobs/create.go @@ -4,7 +4,7 @@ import ( "context" "github.com/rancher/shepherd/clients/rancher" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/timeouts" "github.com/rancher/shepherd/extensions/unstructured" "github.com/rancher/shepherd/pkg/api/scheme" "github.com/rancher/shepherd/pkg/wait" @@ -67,7 +67,7 @@ func CreateCronJob(client *rancher.Client, clusterName, cronJobName, namespace, watchInterface, err := cronJobResource.Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + unstructuredResp.GetName(), - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { diff --git a/extensions/kubeapi/workloads/daemonsets/create.go b/extensions/kubeapi/workloads/daemonsets/create.go index 18db7471..f3543112 100644 --- a/extensions/kubeapi/workloads/daemonsets/create.go +++ b/extensions/kubeapi/workloads/daemonsets/create.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/rancher/shepherd/clients/rancher" + defaultlabels "github.com/rancher/shepherd/extensions/defaults/labels" "github.com/rancher/shepherd/extensions/unstructured" "github.com/rancher/shepherd/pkg/api/scheme" appv1 "k8s.io/api/apps/v1" @@ -20,7 +21,7 @@ func CreateDaemonSet(client *rancher.Client, clusterName, daemonSetName, namespa } labels := map[string]string{} - labels["workload.user.cattle.io/workloadselector"] = fmt.Sprintf("apps.daemonset-%v-%v", namespace, daemonSetName) + labels[defaultlabels.WorkloadSelector] = fmt.Sprintf("apps.daemonset-%v-%v", namespace, daemonSetName) template.ObjectMeta = metav1.ObjectMeta{ Labels: labels, diff --git a/extensions/kubeapi/workloads/deployments/create.go b/extensions/kubeapi/workloads/deployments/create.go index 42e3038d..df348ca9 100644 --- a/extensions/kubeapi/workloads/deployments/create.go +++ b/extensions/kubeapi/workloads/deployments/create.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/rancher/shepherd/clients/rancher" + defaultlabels "github.com/rancher/shepherd/extensions/defaults/labels" "github.com/rancher/shepherd/extensions/unstructured" "github.com/rancher/shepherd/pkg/api/scheme" appv1 "k8s.io/api/apps/v1" @@ -29,7 +30,7 @@ func CreateDeployment(client *rancher.Client, clusterName, deploymentName, names } labels := map[string]string{} - labels["workload.user.cattle.io/workloadselector"] = fmt.Sprintf("apps.deployment-%v-%v", namespace, deploymentName) + labels[defaultlabels.WorkloadSelector] = fmt.Sprintf("apps.deployment-%v-%v", namespace, deploymentName) template.ObjectMeta = metav1.ObjectMeta{ Labels: labels, diff --git a/extensions/kubeapi/workloads/jobs/create.go b/extensions/kubeapi/workloads/jobs/create.go index 0a388f5b..f043ee5f 100644 --- a/extensions/kubeapi/workloads/jobs/create.go +++ b/extensions/kubeapi/workloads/jobs/create.go @@ -4,7 +4,7 @@ import ( "context" "github.com/rancher/shepherd/clients/rancher" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/timeouts" "github.com/rancher/shepherd/extensions/unstructured" "github.com/rancher/shepherd/pkg/api/scheme" "github.com/rancher/shepherd/pkg/wait" @@ -61,7 +61,7 @@ func CreateJob(client *rancher.Client, clusterName, jobName, namespace string, t watchInterface, err := jobResource.Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + unstructuredResp.GetName(), - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { diff --git a/extensions/kubectl/command.go b/extensions/kubectl/command.go index 8f2ff86a..088a9629 100644 --- a/extensions/kubectl/command.go +++ b/extensions/kubectl/command.go @@ -10,9 +10,9 @@ import ( "github.com/rancher/shepherd/clients/rancher" management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" "github.com/rancher/shepherd/extensions/kubeconfig" "github.com/rancher/shepherd/extensions/workloads" - "github.com/rancher/shepherd/extensions/workloads/pods" corev1 "k8s.io/api/core/v1" ) @@ -97,7 +97,7 @@ func Command(client *rancher.Client, yamlContent *management.ImportClusterYamlIn } steveClient := client.Steve - pods, err := steveClient.SteveType(pods.PodResourceSteveType).NamespacedSteveClient(Namespace).List(nil) + pods, err := steveClient.SteveType(stevetypes.Pod).NamespacedSteveClient(Namespace).List(nil) if err != nil { return "", err } diff --git a/extensions/machinepools/machinepools.go b/extensions/machinepools/machinepools.go index 0cfd6ea2..652f87b2 100644 --- a/extensions/machinepools/machinepools.go +++ b/extensions/machinepools/machinepools.go @@ -10,7 +10,8 @@ import ( rkev1 "github.com/rancher/rancher/pkg/apis/rke.cattle.io/v1" "github.com/rancher/shepherd/clients/rancher" v1 "github.com/rancher/shepherd/clients/rancher/v1" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" nodestat "github.com/rancher/shepherd/extensions/nodes" "github.com/sirupsen/logrus" corev1 "k8s.io/api/core/v1" @@ -49,7 +50,7 @@ func MatchNodeRolesToMachinePool(nodeRoles NodeRoles, machinePools []apisV1.RKEM // updateMachinePoolQuantity is a helper method that will update the desired machine pool with the latest quantity. func updateMachinePoolQuantity(client *rancher.Client, cluster *v1.SteveAPIObject, nodeRoles NodeRoles) (*v1.SteveAPIObject, error) { - updateCluster, err := client.Steve.SteveType("provisioning.cattle.io.cluster").ByID(cluster.ID) + updateCluster, err := client.Steve.SteveType(stevetypes.Provisioning).ByID(cluster.ID) if err != nil { return nil, err } @@ -67,19 +68,19 @@ func updateMachinePoolQuantity(client *rancher.Client, cluster *v1.SteveAPIObjec updatedCluster.Spec.RKEConfig.MachinePools[machineConfig].Quantity = &newQuantity logrus.Infof("Scaling the machine pool to %v total nodes", newQuantity) - cluster, err = client.Steve.SteveType("provisioning.cattle.io.cluster").Update(cluster, updatedCluster) + cluster, err = client.Steve.SteveType(stevetypes.Provisioning).Update(cluster, updatedCluster) if err != nil { return nil, err } - err = kwait.Poll(500*time.Millisecond, defaults.TenMinuteTimeout, func() (done bool, err error) { - clusterResp, err := client.Steve.SteveType("provisioning.cattle.io.cluster").ByID(cluster.ID) + err = kwait.Poll(500*time.Millisecond, timeouts.TenMinuteTimeout, func() (done bool, err error) { + clusterResp, err := client.Steve.SteveType(stevetypes.Provisioning).ByID(cluster.ID) if err != nil { return false, err } if clusterResp.ObjectMeta.State.Name == active && - nodestat.AllManagementNodeReady(client, cluster.ID, defaults.ThirtyMinuteTimeout) == nil { + nodestat.AllManagementNodeReady(client, cluster.ID, timeouts.ThirtyMinuteTimeout) == nil { return true, nil } diff --git a/extensions/namespaces/create.go b/extensions/namespaces/create.go index b24e7ed5..5be5f241 100644 --- a/extensions/namespaces/create.go +++ b/extensions/namespaces/create.go @@ -9,7 +9,9 @@ import ( "github.com/rancher/shepherd/clients/rancher" management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" v1 "github.com/rancher/shepherd/clients/rancher/v1" - "github.com/rancher/shepherd/extensions/defaults" + defaultAnnotations "github.com/rancher/shepherd/extensions/defaults/annotations" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" "github.com/rancher/shepherd/extensions/kubeapi/namespaces" "github.com/rancher/shepherd/pkg/api/scheme" "github.com/rancher/shepherd/pkg/wait" @@ -33,7 +35,7 @@ func CreateNamespace(client *rancher.Client, namespaceName, containerDefaultReso annotations = make(map[string]string) } if containerDefaultResourceLimit != "" { - annotations["field.cattle.io/containerDefaultResourceLimit"] = containerDefaultResourceLimit + annotations[defaultAnnotations.ContainerResourceLimit] = containerDefaultResourceLimit } if project != nil { annotations["field.cattle.io/projectId"] = project.ID @@ -51,7 +53,7 @@ func CreateNamespace(client *rancher.Client, namespaceName, containerDefaultReso return nil, err } - nameSpaceClient := steveClient.SteveType(NamespaceSteveType) + nameSpaceClient := steveClient.SteveType(stevetypes.Namespace) resp, err := nameSpaceClient.Create(namespace) if err != nil { @@ -73,7 +75,7 @@ func CreateNamespace(client *rancher.Client, namespaceName, containerDefaultReso clusterRoleWatch, err := clusterRoleResource.Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + fmt.Sprintf("%s-namespaces-edit", projectID), - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { @@ -108,7 +110,7 @@ func CreateNamespace(client *rancher.Client, namespaceName, containerDefaultReso return err } - nameSpaceClient = steveClient.SteveType(NamespaceSteveType) + nameSpaceClient = steveClient.SteveType(stevetypes.Namespace) err := nameSpaceClient.Delete(resp) if errors.IsNotFound(err) { return nil @@ -120,7 +122,7 @@ func CreateNamespace(client *rancher.Client, namespaceName, containerDefaultReso adminNamespaceResource := adminDynamicClient.Resource(namespaces.NamespaceGroupVersionResource).Namespace("") watchInterface, err := adminNamespaceResource.Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + resp.Name, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { diff --git a/extensions/nodes/node_status.go b/extensions/nodes/node_status.go index 17bfb031..29febf6d 100644 --- a/extensions/nodes/node_status.go +++ b/extensions/nodes/node_status.go @@ -9,6 +9,10 @@ import ( rkev1 "github.com/rancher/rancher/pkg/apis/rke.cattle.io/v1" "github.com/rancher/shepherd/clients/rancher" v1 "github.com/rancher/shepherd/clients/rancher/v1" + "github.com/rancher/shepherd/extensions/defaults/annotations" + "github.com/rancher/shepherd/extensions/defaults/namespaces" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/util/wait" ) @@ -19,15 +23,11 @@ const ( errorState = "error" machineSteveResourceType = "cluster.x-k8s.io.machine" machineSteveAnnotation = "cluster.x-k8s.io/machine" - fleetNamespace = "fleet-default" - etcdLabel = "rke.cattle.io/etcd-role" - clusterLabel = "cluster.x-k8s.io/cluster-name" PollInterval = time.Duration(5 * time.Second) PollTimeout = time.Duration(15 * time.Minute) oneSecondInterval = time.Duration(1 * time.Second) - fiveMinuteTimeout = time.Duration(5 * time.Minute) httpNotFound = "404 Not Found" ) @@ -88,8 +88,8 @@ func AllMachineReady(client *rancher.Client, clusterID string, timeout time.Dura for _, node := range nodes.Data { machine, err := client.Steve. - SteveType(machineSteveResourceType). - ByID(fleetNamespace + "/" + node.Annotations[machineSteveAnnotation]) + SteveType(stevetypes.Machine). + ByID(namespaces.Fleet + "/" + node.Annotations[annotations.Machine]) if err != nil { return false, err } @@ -120,7 +120,7 @@ func AllMachineReady(client *rancher.Client, clusterID string, timeout time.Dura func AllNodeDeleted(client *rancher.Client, ClusterID string) error { ctx := context.Background() err := wait.PollUntilContextTimeout( - ctx, oneSecondInterval, fiveMinuteTimeout, true, func(ctx context.Context) (bool, error) { + ctx, oneSecondInterval, timeouts.FiveMinuteTimeout, true, func(ctx context.Context) (bool, error) { nodes, err := client.Management.Node.ListAll(&types.ListOpts{ Filters: map[string]interface{}{ "clusterId": ClusterID, @@ -149,7 +149,7 @@ func IsNodeReplaced(client *rancher.Client, oldMachineID string, clusterID strin ctx := context.Background() err := wait.PollUntilContextTimeout( - ctx, oneSecondInterval, PollTimeout, true, func(ctx context.Context) (bool, error) { + ctx, oneSecondInterval, timeouts.FifteenMinuteTimeout, true, func(ctx context.Context) (bool, error) { machines, err := client.Management.Node.ListAll(&types.ListOpts{Filters: map[string]interface{}{ "clusterId": clusterID, }}) @@ -188,7 +188,7 @@ func Isv1NodeConditionMet(client *rancher.Client, machineID, clusterID, conditio ctx := context.Background() err = wait.PollUntilContextTimeout( - ctx, PollInterval, PollTimeout, true, func(ctx context.Context) (bool, error) { + ctx, PollInterval, timeouts.FifteenMinuteTimeout, true, func(ctx context.Context) (bool, error) { refreshedMachine, err := steveclient.SteveType("node").ByID(machineID) if err != nil { if strings.Contains(err.Error(), httpNotFound) { diff --git a/extensions/pipeline/setup.go b/extensions/pipeline/setup.go index a140316a..66614cbc 100644 --- a/extensions/pipeline/setup.go +++ b/extensions/pipeline/setup.go @@ -10,6 +10,7 @@ import ( "github.com/rancher/shepherd/clients/rancher" management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" v1 "github.com/rancher/shepherd/clients/rancher/v1" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" "github.com/rancher/shepherd/extensions/kubeapi/cluster" "github.com/rancher/shepherd/extensions/token" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -93,7 +94,7 @@ func UpdateEULA(adminClient *rancher.Client) error { return false, err } - urlSettingResp, err = steveClient.SteveType("management.cattle.io.setting").ByID("server-url") + urlSettingResp, err = steveClient.SteveType(stevetypes.ManagementSetting).ByID("server-url") if err != nil { serverURL = err return false, nil @@ -119,7 +120,7 @@ func UpdateEULA(adminClient *rancher.Client) error { urlSetting.Value = fmt.Sprintf("https://%s", adminClient.RancherConfig.Host) - _, err = steveClient.SteveType("management.cattle.io.setting").Update(urlSettingResp, urlSetting) + _, err = steveClient.SteveType(stevetypes.ManagementSetting).Update(urlSettingResp, urlSetting) if err != nil { return err } @@ -135,7 +136,7 @@ func UpdateEULA(adminClient *rancher.Client) error { var pollError error err = kwait.Poll(500*time.Millisecond, 2*time.Minute, func() (done bool, err error) { - _, err = steveClient.SteveType("management.cattle.io.setting").Create(settingEULA) + _, err = steveClient.SteveType(stevetypes.ManagementSetting).Create(settingEULA) if err != nil && !strings.Contains(err.Error(), "409 Conflict") { pollError = err @@ -143,7 +144,7 @@ func UpdateEULA(adminClient *rancher.Client) error { } urlSetting := &v3.Setting{} - urlSettingResp, err := steveClient.SteveType("management.cattle.io.setting").ByID("server-url") + urlSettingResp, err := steveClient.SteveType(stevetypes.ManagementSetting).ByID("server-url") if err != nil { return false, err } diff --git a/extensions/provisioning/creates.go b/extensions/provisioning/creates.go index f82313c5..b217d81f 100644 --- a/extensions/provisioning/creates.go +++ b/extensions/provisioning/creates.go @@ -20,8 +20,10 @@ import ( "github.com/rancher/shepherd/extensions/clusters/aks" "github.com/rancher/shepherd/extensions/clusters/eks" "github.com/rancher/shepherd/extensions/clusters/gke" - "github.com/rancher/shepherd/extensions/defaults" - "github.com/rancher/shepherd/extensions/etcdsnapshot" + "github.com/rancher/shepherd/extensions/defaults/annotations" + "github.com/rancher/shepherd/extensions/defaults/namespaces" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" k3sHardening "github.com/rancher/shepherd/extensions/hardening/k3s" rke2Hardening "github.com/rancher/shepherd/extensions/hardening/rke2" "github.com/rancher/shepherd/extensions/machinepools" @@ -71,7 +73,7 @@ func CreateProvisioningCluster(client *rancher.Client, provider Provider, cluste clusterName := namegen.AppendRandomString(provider.Name.String()) generatedPoolName := fmt.Sprintf("nc-%s-pool1-", clusterName) - machinePoolConfigs := provider.MachinePoolFunc(generatedPoolName, namespace) + machinePoolConfigs := provider.MachinePoolFunc(generatedPoolName, namespaces.Fleet) var machinePoolResponses []v1.SteveAPIObject for _, machinePoolConfig := range machinePoolConfigs { @@ -147,7 +149,7 @@ func CreateProvisioningCluster(client *rancher.Client, provider Provider, cluste } createdCluster, err := adminClient.Steve. - SteveType(clusters.ProvisioningSteveResourceType). + SteveType(stevetypes.Provisioning). ByID(namespace + "/" + clusterName) return createdCluster, err } @@ -213,7 +215,7 @@ func CreateProvisioningCustomCluster(client *rancher.Client, externalNodeProvide return nil, err } - customCluster, err := client.Steve.SteveType(etcdsnapshot.ProvisioningSteveResouceType).ByID(clusterResp.ID) + customCluster, err := client.Steve.SteveType(stevetypes.Provisioning).ByID(clusterResp.ID) if err != nil { return nil, err } @@ -236,7 +238,7 @@ func CreateProvisioningCustomCluster(client *rancher.Client, externalNodeProvide result, err := kubeProvisioningClient.Clusters(namespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + clusterName, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) if err != nil { return nil, err @@ -496,7 +498,7 @@ func CreateProvisioningAirgapCustomCluster(client *rancher.Client, clustersConfi return nil, err } - customCluster, err := client.Steve.SteveType(clusters.ProvisioningSteveResourceType).ByID(clusterResp.ID) + customCluster, err := client.Steve.SteveType(stevetypes.Provisioning).ByID(clusterResp.ID) if err != nil { return nil, err } @@ -539,7 +541,7 @@ func CreateProvisioningAirgapCustomCluster(client *rancher.Client, clustersConfi } } - createdCluster, err := client.Steve.SteveType(clusters.ProvisioningSteveResourceType).ByID(namespace + "/" + clusterName) + createdCluster, err := client.Steve.SteveType(stevetypes.Provisioning).ByID(namespace + "/" + clusterName) return createdCluster, err } @@ -805,13 +807,13 @@ func AddRKE2K3SCustomClusterNodes(client *rancher.Client, cluster *v1.SteveAPIOb logrus.Infof(output) } - err = kwait.Poll(500*time.Millisecond, defaults.TenMinuteTimeout, func() (done bool, err error) { - clusterResp, err := client.Steve.SteveType(clusters.ProvisioningSteveResourceType).ByID(cluster.ID) + err = kwait.Poll(500*time.Millisecond, timeouts.TenMinuteTimeout, func() (done bool, err error) { + clusterResp, err := client.Steve.SteveType(stevetypes.Provisioning).ByID(cluster.ID) if err != nil { return false, err } - if clusterResp.ObjectMeta.State.Name == active && nodestat.AllManagementNodeReady(client, cluster.ID, defaults.ThirtyMinuteTimeout) == nil { + if clusterResp.ObjectMeta.State.Name == active && nodestat.AllManagementNodeReady(client, cluster.ID, timeouts.ThirtyMinuteTimeout) == nil { return true, nil } return false, nil @@ -830,7 +832,7 @@ func DeleteRKE2K3SCustomClusterNodes(client *rancher.Client, clusterID string, c return err } - nodesSteveObjList, err := steveclient.SteveType("node").List(nil) + nodesSteveObjList, err := steveclient.SteveType(stevetypes.Node).List(nil) if err != nil { return err } @@ -838,19 +840,19 @@ func DeleteRKE2K3SCustomClusterNodes(client *rancher.Client, clusterID string, c for _, nodeToDelete := range nodesToDelete { for _, node := range nodesSteveObjList.Data { if node.Annotations[internalIP] == nodeToDelete.PrivateIPAddress { - machine, err := client.Steve.SteveType(machineSteveResourceType).ByID(namespace + "/" + node.Annotations[machineNameAnnotation]) + machine, err := client.Steve.SteveType(stevetypes.Machine).ByID(namespace + "/" + node.Annotations[annotations.Machine]) if err != nil { return err } logrus.Infof("Deleting node %s from cluster %s", nodeToDelete.NodeID, cluster.Name) - err = client.Steve.SteveType(machineSteveResourceType).Delete(machine) + err = client.Steve.SteveType(stevetypes.Machine).Delete(machine) if err != nil { return err } - err = kwait.Poll(500*time.Millisecond, defaults.TenMinuteTimeout, func() (done bool, err error) { - _, err = client.Steve.SteveType(machineSteveResourceType).ByID(machine.ID) + err = kwait.Poll(500*time.Millisecond, timeouts.TenMinuteTimeout, func() (done bool, err error) { + _, err = client.Steve.SteveType(stevetypes.Machine).ByID(machine.ID) if err != nil { logrus.Infof("Node has successfully been deleted!") return true, nil @@ -887,13 +889,13 @@ func AddRKE1CustomClusterNodes(client *rancher.Client, cluster *management.Clust logrus.Infof(output) } - err = kwait.Poll(500*time.Millisecond, defaults.TenMinuteTimeout, func() (done bool, err error) { + err = kwait.Poll(500*time.Millisecond, timeouts.TenMinuteTimeout, func() (done bool, err error) { clusterResp, err := client.Management.Cluster.ByID(cluster.ID) if err != nil { return false, err } - if clusterResp.State == active && nodestat.AllManagementNodeReady(client, cluster.ID, defaults.ThirtyMinuteTimeout) == nil { + if clusterResp.State == active && nodestat.AllManagementNodeReady(client, cluster.ID, timeouts.ThirtyMinuteTimeout) == nil { return true, nil } return false, nil @@ -928,7 +930,7 @@ func DeleteRKE1CustomClusterNodes(client *rancher.Client, cluster *management.Cl return err } - err = kwait.Poll(500*time.Millisecond, defaults.TenMinuteTimeout, func() (done bool, err error) { + err = kwait.Poll(500*time.Millisecond, timeouts.TenMinuteTimeout, func() (done bool, err error) { _, err = client.Management.Node.ByID(machine.ID) if err != nil { logrus.Infof("Node has successfully been deleted!") diff --git a/extensions/provisioning/ssh.go b/extensions/provisioning/ssh.go index 12b09406..6c583dc5 100644 --- a/extensions/provisioning/ssh.go +++ b/extensions/provisioning/ssh.go @@ -12,7 +12,9 @@ import ( "time" "github.com/rancher/shepherd/clients/rancher" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/namespaces" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" extnodes "github.com/rancher/shepherd/extensions/nodes" "github.com/rancher/shepherd/extensions/provisioninginput" "github.com/rancher/shepherd/pkg/nodes" @@ -28,7 +30,6 @@ const ( nodeReboot provisioninginput.SSHTestCase = "NodeReboot" activeState = "active" runningState = "running" - fleetNamespace = "fleet-default" ) // CallSSHTestByName tests the ssh tests specified in the provisioninginput config clusterSSHTests field. @@ -59,8 +60,8 @@ func CallSSHTestByName(testCase provisioninginput.SSHTestCase, node *nodes.Node, return err } // Verify machine shuts down within five minutes, shutting down should not take longer than that depending on the ami - err = wait.Poll(1*time.Second, defaults.FiveMinuteTimeout, func() (bool, error) { - newNode, err := client.Steve.SteveType(machineSteveResourceType).ByID(fleetNamespace + "/" + machineName) + err = wait.Poll(1*time.Second, timeouts.FiveMinuteTimeout, func() (bool, error) { + newNode, err := client.Steve.SteveType(stevetypes.Machine).ByID(namespaces.Fleet + "/" + machineName) if err != nil { return false, err } @@ -74,7 +75,7 @@ func CallSSHTestByName(testCase provisioninginput.SSHTestCase, node *nodes.Node, return err } - err = extnodes.AllMachineReady(client, clusterID, defaults.TenMinuteTimeout) + err = extnodes.AllMachineReady(client, clusterID, timeouts.TenMinuteTimeout) if err != nil { logrus.Errorf("Node %s failed to reboot successfully", node.PublicIPAddress) return err diff --git a/extensions/provisioning/verify.go b/extensions/provisioning/verify.go index c175ee26..a0439308 100644 --- a/extensions/provisioning/verify.go +++ b/extensions/provisioning/verify.go @@ -15,7 +15,9 @@ import ( steveV1 "github.com/rancher/shepherd/clients/rancher/v1" "github.com/rancher/shepherd/extensions/clusters" "github.com/rancher/shepherd/extensions/clusters/bundledclusters" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/annotations" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" "github.com/rancher/shepherd/extensions/etcdsnapshot" "github.com/rancher/shepherd/extensions/kubeconfig" nodestat "github.com/rancher/shepherd/extensions/nodes" @@ -40,8 +42,6 @@ import ( const ( logMessageKubernetesVersion = "Validating the current version is the upgraded one" hostnameLimit = 63 - machineNameAnnotation = "cluster.x-k8s.io/machine" - machineSteveResourceType = "cluster.x-k8s.io.machine" onDemandPrefix = "on-demand-" ) @@ -55,7 +55,7 @@ func VerifyRKE1Cluster(t *testing.T, client *rancher.Client, clustersConfig *clu watchInterface, err := adminClient.GetManagementWatchInterface(management.ClusterType, metav1.ListOptions{ FieldSelector: "metadata.name=" + cluster.ID, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) require.NoError(t, err) @@ -69,7 +69,7 @@ func VerifyRKE1Cluster(t *testing.T, client *rancher.Client, clustersConfig *clu require.NoError(t, err) assert.NotEmpty(t, clusterToken) - err = nodestat.AllManagementNodeReady(client, cluster.ID, defaults.ThirtyMinuteTimeout) + err = nodestat.AllManagementNodeReady(client, cluster.ID, timeouts.ThirtyMinuteTimeout) require.NoError(t, err) if clustersConfig.PSACT == string(provisioninginput.RancherPrivileged) || clustersConfig.PSACT == string(provisioninginput.RancherRestricted) || clustersConfig.PSACT == string(provisioninginput.RancherBaseline) { @@ -112,7 +112,7 @@ func VerifyCluster(t *testing.T, client *rancher.Client, clustersConfig *cluster watchInterface, err := kubeProvisioningClient.Clusters(namespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + cluster.Name, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) require.NoError(t, err) @@ -124,7 +124,7 @@ func VerifyCluster(t *testing.T, client *rancher.Client, clustersConfig *cluster require.NoError(t, err) assert.NotEmpty(t, clusterToken) - err = nodestat.AllMachineReady(client, cluster.ID, defaults.ThirtyMinuteTimeout) + err = nodestat.AllMachineReady(client, cluster.ID, timeouts.ThirtyMinuteTimeout) require.NoError(t, err) status := &provv1.ClusterStatus{} @@ -180,7 +180,7 @@ func VerifyHostedCluster(t *testing.T, client *rancher.Client, cluster *manageme watchInterface, err := adminClient.GetManagementWatchInterface(management.ClusterType, metav1.ListOptions{ FieldSelector: "metadata.name=" + cluster.ID, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) require.NoError(t, err) @@ -193,7 +193,7 @@ func VerifyHostedCluster(t *testing.T, client *rancher.Client, cluster *manageme require.NoError(t, err) assert.NotEmpty(t, clusterToken) - err = nodestat.AllManagementNodeReady(client, cluster.ID, defaults.ThirtyMinuteTimeout) + err = nodestat.AllManagementNodeReady(client, cluster.ID, timeouts.ThirtyMinuteTimeout) require.NoError(t, err) podErrors := pods.StatusPods(client, cluster.ID) @@ -210,7 +210,7 @@ func VerifyDeleteRKE1Cluster(t *testing.T, client *rancher.Client, clusterID str watchInterface, err := adminClient.GetManagementWatchInterface(management.ClusterType, metav1.ListOptions{ FieldSelector: "metadata.name=" + clusterID, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) require.NoError(t, err) @@ -231,7 +231,7 @@ func VerifyDeleteRKE1Cluster(t *testing.T, client *rancher.Client, clusterID str // VerifyDeleteRKE2K3SCluster validates that a non-rke1 cluster and its resources are deleted. func VerifyDeleteRKE2K3SCluster(t *testing.T, client *rancher.Client, clusterID string) { - cluster, err := client.Steve.SteveType("provisioning.cattle.io.cluster").ByID(clusterID) + cluster, err := client.Steve.SteveType(stevetypes.Provisioning).ByID(clusterID) require.NoError(t, err) adminClient, err := rancher.NewClient(client.RancherConfig.AdminToken, client.Session) @@ -242,7 +242,7 @@ func VerifyDeleteRKE2K3SCluster(t *testing.T, client *rancher.Client, clusterID watchInterface, err := provKubeClient.Clusters(namespace).Watch(context.TODO(), metav1.ListOptions{ FieldSelector: "metadata.name=" + cluster.Name, - TimeoutSeconds: &defaults.WatchTimeoutSeconds, + TimeoutSeconds: timeouts.WatchTimeoutSeconds(), }) require.NoError(t, err) @@ -337,7 +337,7 @@ func VerifyHostnameLength(t *testing.T, client *rancher.Client, clusterObject *s query2, err := url.ParseQuery(fmt.Sprintf("labelSelector=%s=%s", capi.MachineDeploymentNameLabel, md.Name)) require.NoError(t, err) - machineResp, err := client.Steve.SteveType(machineSteveResourceType).List(query2) + machineResp, err := client.Steve.SteveType(stevetypes.Machine).List(query2) require.NoError(t, err) assert.True(t, len(machineResp.Data) > 0) @@ -401,7 +401,7 @@ func VerifySnapshots(client *rancher.Client, localclusterID string, clusterName var snapshotToBeRestored string var snapshotNameList []string s3Prefix := onDemandPrefix + clusterName - err = kwait.PollUntilContextTimeout(context.TODO(), 5*time.Second, defaults.FiveMinuteTimeout, true, func(ctx context.Context) (done bool, err error) { + err = kwait.PollUntilContextTimeout(context.TODO(), 5*time.Second, timeouts.FiveMinuteTimeout, true, func(ctx context.Context) (done bool, err error) { if isRKE1 { snapshotObjectList, err := etcdsnapshot.GetRKE1Snapshots(client, clusterName) if err != nil { @@ -460,7 +460,7 @@ func VerifySSHTests(t *testing.T, client *rancher.Client, clusterObject *steveV1 steveClient, err := client.Steve.ProxyDownstream(clusterID) require.NoError(t, err) - nodesSteveObjList, err := steveClient.SteveType("node").List(nil) + nodesSteveObjList, err := steveClient.SteveType(stevetypes.Node).List(nil) require.NoError(t, err) sshUser, err := sshkeys.GetSSHUser(client, clusterObject) @@ -471,7 +471,7 @@ func VerifySSHTests(t *testing.T, client *rancher.Client, clusterObject *steveV1 clusterNode, err := sshkeys.GetSSHNodeFromMachine(client, sshUser, &machine) require.NoError(t, err) - machineName := machine.Annotations[machineNameAnnotation] + machineName := machine.Annotations[annotations.Machine] err = CallSSHTestByName(tests, clusterNode, client, clusterID, machineName) require.NoError(t, err) diff --git a/extensions/psact/createdeployment.go b/extensions/psact/createdeployment.go index e22fe7f6..04b80af9 100644 --- a/extensions/psact/createdeployment.go +++ b/extensions/psact/createdeployment.go @@ -7,6 +7,8 @@ import ( "github.com/rancher/shepherd/clients/rancher" steveV1 "github.com/rancher/shepherd/clients/rancher/v1" + defaultlabels "github.com/rancher/shepherd/extensions/defaults/labels" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" "github.com/rancher/shepherd/extensions/provisioninginput" "github.com/rancher/shepherd/extensions/workloads" namegenerator "github.com/rancher/shepherd/pkg/namegenerator" @@ -29,7 +31,7 @@ const ( // deployment should successfully create. If the PSACT value is rancher-unprivileged, then the deployment should fail to create. func CreateNginxDeployment(client *rancher.Client, clusterID string, psact string) error { labels := map[string]string{} - labels["workload.user.cattle.io/workloadselector"] = fmt.Sprintf("apps.deployment-%v-%v", namespace, workload) + labels[defaultlabels.WorkloadSelector] = fmt.Sprintf("apps.deployment-%v-%v", namespace, workload) containerTemplate := workloads.NewContainer(containerName, imageName, v1.PullAlways, []v1.VolumeMount{}, []v1.EnvFromSource{}, nil, nil, nil) podTemplate := workloads.NewPodTemplate([]v1.Container{containerTemplate}, []v1.Volume{}, []v1.LocalObjectReference{}, labels) @@ -41,12 +43,12 @@ func CreateNginxDeployment(client *rancher.Client, clusterID string, psact strin } // If the deployment already exists, then create a new deployment with a different name to avoid a naming conflict. - if _, err := steveclient.SteveType(workloads.DeploymentSteveType).ByID(deploymentTemplate.Namespace + "/" + deploymentTemplate.Name); err == nil { + if _, err := steveclient.SteveType(stevetypes.Deployment).ByID(deploymentTemplate.Namespace + "/" + deploymentTemplate.Name); err == nil { deploymentTemplate.Name = deploymentTemplate.Name + "-" + namegenerator.RandStringLower(5) } logrus.Infof("Creating deployment %s", deploymentTemplate.Name) - _, err = steveclient.SteveType(workloads.DeploymentSteveType).Create(deploymentTemplate) + _, err = steveclient.SteveType(stevetypes.Deployment).Create(deploymentTemplate) if err != nil { return err } @@ -57,7 +59,7 @@ func CreateNginxDeployment(client *rancher.Client, clusterID string, psact strin return false, err } - deploymentResp, err := steveclient.SteveType(workloads.DeploymentSteveType).ByID(deploymentTemplate.Namespace + "/" + deploymentTemplate.Name) + deploymentResp, err := steveclient.SteveType(stevetypes.Deployment).ByID(deploymentTemplate.Namespace + "/" + deploymentTemplate.Name) if err != nil { // We don't want to return the error so we don't exit the poll too soon. // There could be delay of when the deployment is created. @@ -88,13 +90,13 @@ func CreateNginxDeployment(client *rancher.Client, clusterID string, psact strin return err } - deploymentResp, err := steveclient.SteveType(workloads.DeploymentSteveType).ByID(deploymentTemplate.Namespace + "/" + deploymentTemplate.Name) + deploymentResp, err := steveclient.SteveType(stevetypes.Deployment).ByID(deploymentTemplate.Namespace + "/" + deploymentTemplate.Name) if err != nil { return err } logrus.Infof("Deleting deployment %s", deploymentResp.Name) - err = steveclient.SteveType(workloads.DeploymentSteveType).Delete(deploymentResp) + err = steveclient.SteveType(stevetypes.Deployment).Delete(deploymentResp) if err != nil { return err } diff --git a/extensions/rancherleader/rancherleader.go b/extensions/rancherleader/rancherleader.go index 8aebc1b3..db2b4323 100644 --- a/extensions/rancherleader/rancherleader.go +++ b/extensions/rancherleader/rancherleader.go @@ -5,6 +5,8 @@ import ( "github.com/rancher/shepherd/clients/rancher" v1 "github.com/rancher/shepherd/clients/rancher/v1" + "github.com/rancher/shepherd/extensions/defaults/namespaces" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" coordinationv1 "k8s.io/api/coordination/v1" ) @@ -17,7 +19,7 @@ const ( // GetRancherLeaderPodName is a helper function to retrieve the name of the rancher leader pod func GetRancherLeaderPodName(client *rancher.Client) (string, error) { query := url.Values{"fieldSelector": {"metadata.name=" + LeaseName}} - lease, err := client.Steve.SteveType(LeaseSteveType).NamespacedSteveClient(KubeSystemNamespace).List(query) + lease, err := client.Steve.SteveType(stevetypes.CoordinationLease).NamespacedSteveClient(namespaces.KubeSystem).List(query) if err != nil { return "", err } diff --git a/extensions/rbac/verify.go b/extensions/rbac/verify.go index 6d42b603..b92fe0af 100644 --- a/extensions/rbac/verify.go +++ b/extensions/rbac/verify.go @@ -11,7 +11,7 @@ import ( "github.com/rancher/shepherd/clients/rancher" management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" v1 "github.com/rancher/shepherd/clients/rancher/v1" - "github.com/rancher/shepherd/extensions/clusters" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" "github.com/rancher/shepherd/extensions/namespaces" "github.com/rancher/shepherd/extensions/projects" "github.com/rancher/shepherd/extensions/users" @@ -41,14 +41,14 @@ var rgx = regexp.MustCompile(`\[(.*?)\]`) // VerifyGlobalRoleBindingsForUser validates that a global role bindings is created for a user when the user is created func VerifyGlobalRoleBindingsForUser(t *testing.T, user *management.User, adminClient *rancher.Client) { query := url.Values{"filter": {"userName=" + user.ID}} - grbs, err := adminClient.Steve.SteveType("management.cattle.io.globalrolebinding").List(query) + grbs, err := adminClient.Steve.SteveType(stevetypes.GlobalRoleBinding).List(query) require.NoError(t, err) assert.Equal(t, 1, len(grbs.Data)) } // VerifyUserCanListCluster validates a user with the required global permissions are able to/not able to list the clusters in rancher server func VerifyUserCanListCluster(t *testing.T, client, standardClient *rancher.Client, clusterID, role string) { - clusterList, err := standardClient.Steve.SteveType(clusters.ProvisioningSteveResourceType).ListAll(nil) + clusterList, err := standardClient.Steve.SteveType(stevetypes.Provisioning).ListAll(nil) require.NoError(t, err) clusterStatus := &apiV1.ClusterStatus{} @@ -56,7 +56,7 @@ func VerifyUserCanListCluster(t *testing.T, client, standardClient *rancher.Clie require.NoError(t, err) if role == restrictedAdmin { - adminClusterList, err := client.Steve.SteveType(clusters.ProvisioningSteveResourceType).ListAll(nil) + adminClusterList, err := client.Steve.SteveType(stevetypes.Provisioning).ListAll(nil) require.NoError(t, err) assert.Equal(t, (len(adminClusterList.Data) - 1), len(clusterList.Data)) } @@ -139,11 +139,11 @@ func VerifyUserCanListNamespace(t *testing.T, client, standardClient *rancher.Cl steveStandardClient, err := standardClient.Steve.ProxyDownstream(clusterID) require.NoError(t, err) - namespaceListAdmin, err := steveAdminClient.SteveType(namespaces.NamespaceSteveType).List(nil) + namespaceListAdmin, err := steveAdminClient.SteveType(stevetypes.Namespace).List(nil) require.NoError(t, err) sortedNamespaceListAdmin := namespaceListAdmin.Names() - namespaceListNonAdmin, err := steveStandardClient.SteveType(namespaces.NamespaceSteveType).List(nil) + namespaceListNonAdmin, err := steveStandardClient.SteveType(stevetypes.Namespace).List(nil) require.NoError(t, err) sortedNamespaceListNonAdmin := namespaceListNonAdmin.Names() @@ -175,9 +175,9 @@ func VerifyUserCanDeleteNamespace(t *testing.T, client, standardClient *rancher. adminNamespace, err := namespaces.CreateNamespace(client, namespaceName+"-admin", "{}", map[string]string{}, map[string]string{}, project) require.NoError(t, err) - namespaceID, err := steveAdminClient.SteveType(namespaces.NamespaceSteveType).ByID(adminNamespace.ID) + namespaceID, err := steveAdminClient.SteveType(stevetypes.Namespace).ByID(adminNamespace.ID) require.NoError(t, err) - err = steveStandardClient.SteveType(namespaces.NamespaceSteveType).Delete(namespaceID) + err = steveStandardClient.SteveType(stevetypes.Namespace).Delete(namespaceID) switch role { case roleOwner, roleProjectOwner, roleProjectMember, restrictedAdmin: diff --git a/extensions/registries/registries.go b/extensions/registries/registries.go index 07543769..a0fca48b 100644 --- a/extensions/registries/registries.go +++ b/extensions/registries/registries.go @@ -7,6 +7,7 @@ import ( "github.com/rancher/shepherd/clients/rancher" v1 "github.com/rancher/shepherd/clients/rancher/v1" "github.com/rancher/shepherd/extensions/clusters" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" "github.com/rancher/shepherd/extensions/workloads/pods" log "github.com/sirupsen/logrus" corev1 "k8s.io/api/core/v1" @@ -20,7 +21,7 @@ func CheckAllClusterPodsForRegistryPrefix(client *rancher.Client, clusterID, reg return false, err } - steveClient := downstreamClient.SteveType(pods.PodResourceSteveType) + steveClient := downstreamClient.SteveType(stevetypes.Pod) podsList, err := steveClient.List(nil) if err != nil { return false, err diff --git a/extensions/rke1/componentchecks/etcdversion.go b/extensions/rke1/componentchecks/etcdversion.go index 3c9cdfb0..57085c7c 100644 --- a/extensions/rke1/componentchecks/etcdversion.go +++ b/extensions/rke1/componentchecks/etcdversion.go @@ -4,6 +4,9 @@ import ( "strings" "github.com/rancher/shepherd/clients/rancher" + "github.com/rancher/shepherd/extensions/defaults/annotations" + "github.com/rancher/shepherd/extensions/defaults/labels" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" "github.com/rancher/shepherd/pkg/nodes" "github.com/sirupsen/logrus" ) @@ -15,7 +18,7 @@ func CheckETCDVersion(client *rancher.Client, nodes []*nodes.Node, clusterID str return nil, err } - nodesList, err := steveClient.SteveType("node").List(nil) + nodesList, err := steveClient.SteveType(stevetypes.Node).List(nil) if err != nil { return nil, err } @@ -23,8 +26,8 @@ func CheckETCDVersion(client *rancher.Client, nodes []*nodes.Node, clusterID str var etcdResult []string for _, rancherNode := range nodesList.Data { - externalIP := rancherNode.Annotations["rke.cattle.io/external-ip"] - etcdRole := rancherNode.Labels["node-role.kubernetes.io/etcd"] == "true" + externalIP := rancherNode.Annotations[annotations.ExternalIp] + etcdRole := rancherNode.Labels[labels.EtcdRole] == "true" if etcdRole == true { for _, node := range nodes { diff --git a/extensions/rke1/nodepools/nodepools.go b/extensions/rke1/nodepools/nodepools.go index d9d2639b..11003ef3 100644 --- a/extensions/rke1/nodepools/nodepools.go +++ b/extensions/rke1/nodepools/nodepools.go @@ -7,7 +7,7 @@ import ( "github.com/rancher/norman/types" "github.com/rancher/shepherd/clients/rancher" management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/timeouts" nodestat "github.com/rancher/shepherd/extensions/nodes" "github.com/sirupsen/logrus" kwait "k8s.io/apimachinery/pkg/util/wait" @@ -110,13 +110,13 @@ func updateNodePoolQuantity(client *rancher.Client, cluster *management.Cluster, return nil, err } - err = kwait.Poll(500*time.Millisecond, defaults.TenMinuteTimeout, func() (done bool, err error) { + err = kwait.Poll(500*time.Millisecond, timeouts.TenMinuteTimeout, func() (done bool, err error) { clusterResp, err := client.Management.Cluster.ByID(cluster.ID) if err != nil { return false, err } - if clusterResp.State == active && nodestat.AllManagementNodeReady(client, clusterResp.ID, defaults.ThirtyMinuteTimeout) == nil { + if clusterResp.State == active && nodestat.AllManagementNodeReady(client, clusterResp.ID, timeouts.ThirtyMinuteTimeout) == nil { logrus.Infof("Node pool is scaled!") return true, nil } diff --git a/extensions/services/verify.go b/extensions/services/verify.go index 8736bd38..cdd2eec2 100644 --- a/extensions/services/verify.go +++ b/extensions/services/verify.go @@ -7,6 +7,7 @@ import ( "github.com/rancher/shepherd/clients/rancher" v1 "github.com/rancher/shepherd/clients/rancher/v1" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" "github.com/rancher/shepherd/extensions/ingresses" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" @@ -27,7 +28,7 @@ func VerifyAWSLoadBalancer(t *testing.T, client *rancher.Client, serviceLB *v1.S lbHostname := "" err = kwait.Poll(5*time.Second, 1*time.Minute, func() (done bool, err error) { - updateService, err := steveclient.SteveType("service").ByID(serviceLB.ID) + updateService, err := steveclient.SteveType(stevetypes.Service).ByID(serviceLB.ID) if err != nil { return false, nil } diff --git a/extensions/settings/shell.go b/extensions/settings/shell.go index d0921243..b9c99e4b 100644 --- a/extensions/settings/shell.go +++ b/extensions/settings/shell.go @@ -4,6 +4,7 @@ import ( v3 "github.com/rancher/rancher/pkg/apis/management.cattle.io/v3" "github.com/rancher/shepherd/clients/rancher" v1 "github.com/rancher/shepherd/clients/rancher/v1" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" ) // ShellVersion is a helper that gets the shell setting json based on the ID and return the shell image value. @@ -14,7 +15,7 @@ func ShellVersion(client *rancher.Client, clusterID, resourceName string) (strin } shellSetting := &v3.Setting{} - shellSettingResp, err := steveClient.SteveType("management.cattle.io.setting").ByID("shell-image") + shellSettingResp, err := steveClient.SteveType(stevetypes.ManagementSetting).ByID("shell-image") if err != nil { return "", err } diff --git a/extensions/sshkeys/downloadsshkeys.go b/extensions/sshkeys/downloadsshkeys.go index 89255a34..7b7b3566 100644 --- a/extensions/sshkeys/downloadsshkeys.go +++ b/extensions/sshkeys/downloadsshkeys.go @@ -11,6 +11,7 @@ import ( "github.com/rancher/shepherd/clients/rancher" steveV1 "github.com/rancher/shepherd/clients/rancher/v1" "github.com/rancher/shepherd/extensions/clusters" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" kubeapinodes "github.com/rancher/shepherd/extensions/kubeapi/nodes" "github.com/rancher/shepherd/pkg/nodes" corev1 "k8s.io/api/core/v1" @@ -28,7 +29,7 @@ const ( // the ssh key for a particular node. func DownloadSSHKeys(client *rancher.Client, machinePoolNodeName string) ([]byte, error) { machinePoolNodeNameName := fmt.Sprintf("fleet-default/%s", machinePoolNodeName) - machine, err := client.Steve.SteveType(ClusterMachineConstraintResourceSteveType).ByID(machinePoolNodeNameName) + machine, err := client.Steve.SteveType(stevetypes.Machine).ByID(machinePoolNodeNameName) if err != nil { return nil, err } diff --git a/extensions/workloads/create.go b/extensions/workloads/create.go index 37138e08..f5eb9a30 100644 --- a/extensions/workloads/create.go +++ b/extensions/workloads/create.go @@ -2,6 +2,7 @@ package workloads import ( steveV1 "github.com/rancher/shepherd/clients/rancher/v1" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" "github.com/sirupsen/logrus" v1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -16,7 +17,7 @@ const ( // CreateDeploymentWithService is a helper function to create a deployment and service in the downstream cluster. func CreateDeploymentWithService(steveclient *steveV1.Client, wlName string, deployment *v1.Deployment, service corev1.Service) (*steveV1.SteveAPIObject, *steveV1.SteveAPIObject, error) { logrus.Infof("Creating deployment: %s", wlName) - deploymentResp, err := steveclient.SteveType(DeploymentSteveType).Create(deployment) + deploymentResp, err := steveclient.SteveType(stevetypes.Deployment).Create(deployment) if err != nil { logrus.Errorf("Failed to create deployment: %s", wlName) return nil, nil, err @@ -25,7 +26,7 @@ func CreateDeploymentWithService(steveclient *steveV1.Client, wlName string, dep logrus.Infof("Successfully created deployment: %s", wlName) logrus.Infof("Creating service: %s", service.Name) - serviceResp, err := steveclient.SteveType(ServiceType).Create(service) + serviceResp, err := steveclient.SteveType(stevetypes.Service).Create(service) if err != nil { logrus.Errorf("Failed to create service: %s", service.Name) return nil, nil, err diff --git a/extensions/workloads/pods/pod_status.go b/extensions/workloads/pods/pod_status.go index aa1934ff..ff62578d 100644 --- a/extensions/workloads/pods/pod_status.go +++ b/extensions/workloads/pods/pod_status.go @@ -6,7 +6,8 @@ import ( "github.com/rancher/shepherd/clients/rancher" v1 "github.com/rancher/shepherd/clients/rancher/v1" - "github.com/rancher/shepherd/extensions/defaults" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" + "github.com/rancher/shepherd/extensions/defaults/timeouts" corev1 "k8s.io/api/core/v1" kwait "k8s.io/apimachinery/pkg/util/wait" ) @@ -25,8 +26,8 @@ func StatusPods(client *rancher.Client, clusterID string) []error { var podErrors []error - steveClient := downstreamClient.SteveType(PodResourceSteveType) - err = kwait.Poll(5*time.Second, defaults.FifteenMinuteTimeout, func() (done bool, err error) { + steveClient := downstreamClient.SteveType(stevetypes.Pod) + err = kwait.Poll(5*time.Second, timeouts.FifteenMinuteTimeout, func() (done bool, err error) { // emptying pod errors every time we poll so that we don't return stale errors podErrors = []error{} diff --git a/extensions/workloads/pods/verify.go b/extensions/workloads/pods/verify.go index ba40ee6d..84dddd3c 100644 --- a/extensions/workloads/pods/verify.go +++ b/extensions/workloads/pods/verify.go @@ -7,6 +7,7 @@ import ( provv1 "github.com/rancher/rancher/pkg/apis/provisioning.cattle.io/v1" "github.com/rancher/shepherd/clients/rancher" v1 "github.com/rancher/shepherd/clients/rancher/v1" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" appv1 "k8s.io/api/apps/v1" @@ -27,7 +28,7 @@ func VerifyReadyDaemonsetPods(t *testing.T, client *rancher.Client, cluster *v1. daemonsetequals := false err = wait.Poll(500*time.Millisecond, 5*time.Minute, func() (dameonsetequals bool, err error) { - daemonsets, err := client.Steve.SteveType(DaemonsetSteveType).ByID(status.ClusterName) + daemonsets, err := client.Steve.SteveType(stevetypes.Daemonset).ByID(status.ClusterName) require.NoError(t, err) daemonsetsStatusType := &appv1.DaemonSetStatus{} @@ -41,7 +42,7 @@ func VerifyReadyDaemonsetPods(t *testing.T, client *rancher.Client, cluster *v1. }) require.NoError(t, err) - daemonsets, err := client.Steve.SteveType(DaemonsetSteveType).ByID(status.ClusterName) + daemonsets, err := client.Steve.SteveType(stevetypes.Daemonset).ByID(status.ClusterName) require.NoError(t, err) daemonsetsStatusType := &appv1.DaemonSetStatus{} diff --git a/extensions/workloads/template.go b/extensions/workloads/template.go index c51c9d29..91962a1f 100644 --- a/extensions/workloads/template.go +++ b/extensions/workloads/template.go @@ -3,6 +3,7 @@ package workloads import ( "fmt" + "github.com/rancher/shepherd/extensions/defaults/labels" appv1 "k8s.io/api/apps/v1" batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" @@ -48,8 +49,8 @@ func NewDeploymentTemplate(deploymentName string, namespace string, template cor } if isCattleLabeled { - matchLabels["workload.user.cattle.io/workloadselector"] = fmt.Sprintf("apps.deployment-%v-%v", namespace, deploymentName) - template.ObjectMeta.Labels["workload.user.cattle.io/workloadselector"] = fmt.Sprintf("apps.deployment-%v-%v", namespace, deploymentName) + matchLabels[labels.WorkloadSelector] = fmt.Sprintf("apps.deployment-%v-%v", namespace, deploymentName) + template.ObjectMeta.Labels[labels.WorkloadSelector] = fmt.Sprintf("apps.deployment-%v-%v", namespace, deploymentName) } return &appv1.Deployment{ @@ -74,8 +75,8 @@ func NewDaemonSetTemplate(daemonsetName string, namespace string, template corev } if isCattleLabeled { - matchLabels["workload.user.cattle.io/workloadselector"] = fmt.Sprintf("apps.daemonset-%v-%v", namespace, daemonsetName) - template.ObjectMeta.Labels["workload.user.cattle.io/workloadselector"] = fmt.Sprintf("apps.daemonset-%v-%v", namespace, daemonsetName) + matchLabels[labels.WorkloadSelector] = fmt.Sprintf("apps.daemonset-%v-%v", namespace, daemonsetName) + template.ObjectMeta.Labels[labels.WorkloadSelector] = fmt.Sprintf("apps.daemonset-%v-%v", namespace, daemonsetName) } return &appv1.DaemonSet{ diff --git a/extensions/workloads/verify.go b/extensions/workloads/verify.go index 937fdf33..365f462b 100644 --- a/extensions/workloads/verify.go +++ b/extensions/workloads/verify.go @@ -4,6 +4,7 @@ import ( "time" steveV1 "github.com/rancher/shepherd/clients/rancher/v1" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" appv1 "k8s.io/api/apps/v1" kwait "k8s.io/apimachinery/pkg/util/wait" ) @@ -14,7 +15,7 @@ func VerifyDeployment(steveClient *steveV1.Client, deployment *steveV1.SteveAPIO if err != nil { return false, nil } - deploymentResp, err := steveClient.SteveType(DeploymentSteveType).ByID(deployment.Namespace + "/" + deployment.Name) + deploymentResp, err := steveClient.SteveType(stevetypes.Deployment).ByID(deployment.Namespace + "/" + deployment.Name) if err != nil { return false, nil } diff --git a/pkg/codegen/main.go b/pkg/codegen/main.go index 49b4c07e..a762713e 100644 --- a/pkg/codegen/main.go +++ b/pkg/codegen/main.go @@ -11,6 +11,7 @@ import ( catalogv1 "github.com/rancher/rancher/pkg/apis/catalog.cattle.io/v1" provisioningv1 "github.com/rancher/rancher/pkg/apis/provisioning.cattle.io/v1" rkev1 "github.com/rancher/rancher/pkg/apis/rke.cattle.io/v1" + "github.com/rancher/shepherd/extensions/defaults/stevetypes" "github.com/rancher/shepherd/pkg/codegen/generator" managementSchema "github.com/rancher/shepherd/pkg/schemas/management.cattle.io/v3" planv1 "github.com/rancher/system-upgrade-controller/pkg/apis/upgrade.cattle.io/v1" @@ -82,7 +83,7 @@ func main() { clusterAPIVersion := &types.APIVersion{Group: capi.GroupVersion.Group, Version: capi.GroupVersion.Version, Path: "/v1"} generator.GenerateClient(factory.Schemas(clusterAPIVersion).Init(func(schemas *types.Schemas) *types.Schemas { return schemas.MustImportAndCustomize(clusterAPIVersion, capi.Machine{}, func(schema *types.Schema) { - schema.ID = "cluster.x-k8s.io.machine" + schema.ID = stevetypes.Machine }) }), nil) diff --git a/pkg/schemas/mapper/node_address.go b/pkg/schemas/mapper/node_address.go index 4f1794ad..9b2d0e7b 100644 --- a/pkg/schemas/mapper/node_address.go +++ b/pkg/schemas/mapper/node_address.go @@ -3,6 +3,7 @@ package mapper import ( "github.com/rancher/norman/types" "github.com/rancher/norman/types/values" + "github.com/rancher/shepherd/extensions/defaults/annotations" ) const ( @@ -39,7 +40,7 @@ type NodeAddressAnnotationMapper struct { } func (n NodeAddressAnnotationMapper) FromInternal(data map[string]interface{}) { - externalIP, ok := values.GetValue(data, "status", "nodeAnnotations", "rke.cattle.io/external-ip") + externalIP, ok := values.GetValue(data, "status", "nodeAnnotations", annotations.ExternalIp) if ok { data[extIPField] = externalIP }