Skip to content

Commit

Permalink
Constants update
Browse files Browse the repository at this point in the history
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
  • Loading branch information
susesgartner committed Apr 3, 2024
1 parent dd0f1e7 commit a748cb3
Show file tree
Hide file tree
Showing 52 changed files with 347 additions and 250 deletions.
18 changes: 10 additions & 8 deletions clients/k3d/k3d.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand All @@ -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")
}

Expand All @@ -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")
Expand All @@ -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
}

Expand All @@ -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,
})
Expand Down
4 changes: 2 additions & 2 deletions clients/rkecli/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 9 additions & 11 deletions extensions/charts/awsoutoftree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
15 changes: 8 additions & 7 deletions extensions/charts/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions extensions/charts/payloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 9 additions & 9 deletions extensions/charts/ranchergatekeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions extensions/charts/rancheristio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit a748cb3

Please sign in to comment.