Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ linters:
- goconst
- forbidigo
- predeclared
- gochecknoglobals
linters-settings:
revive:
rules:
Expand All @@ -35,6 +36,11 @@ issues:
exclude-dirs:
- pkg/generated/*
- clients/rancher/generated/*
exclude-rules:
#Scheme and v3 schemas are being skipped due to the complexity of updating the global vars used in Shepherd clients
- path: pkg/*
linters:
- gochecknoglobals
exclude-files:
- ^*\.yaml$
- ^*\.yml$
Expand Down
6 changes: 2 additions & 4 deletions clients/dynamic/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,18 @@ type ResourceClient struct {
ts *session.Session
}

var (
func needsCleanup(obj *unstructured.Unstructured) bool {
// some GVKs are special and cannot be cleaned up because they do not exist
// after being created (eg: SelfSubjectAccessReview). We'll not register
// cleanup functions when creating objects of these kinds.
noCleanupGVKs = []schema.GroupVersionKind{
noCleanupGVKs := []schema.GroupVersionKind{
{
Group: "authorization.k8s.io",
Version: "v1",
Kind: "SelfSubjectAccessReview",
},
}
)

func needsCleanup(obj *unstructured.Unstructured) bool {
for _, gvk := range noCleanupGVKs {
if obj.GroupVersionKind() == gvk {
return false
Expand Down
2 changes: 1 addition & 1 deletion clients/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/rancher/shepherd/pkg/session"
)

var helmCmd = "helm_v3"
const helmCmd = "helm_v3"

// InstallChart installs a helm chart using helm CLI.
// Send the helm set command strings such as "--set", "installCRDs=true"
Expand Down
22 changes: 11 additions & 11 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 All @@ -21,8 +23,6 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

var importTimeout = int64(60 * 20)

// CreateK3DCluster creates a minimal k3d cluster and returns a rest config for connecting to the newly created cluster.
// If a name is not given a random one will be generated.
func CreateK3DCluster(ts *session.Session, name, hostname string, servers, agents int) (*rest.Config, error) {
Expand Down Expand Up @@ -102,10 +102,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 +114,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 +132,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.WatchTimeout(timeouts.ThirtyMinute),
})
if err != nil {
return nil, errors.Wrap(err, "CreateAndImportK3DCluster: failed to watch for the imported cluster")
Expand All @@ -144,7 +144,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,9 +164,9 @@ 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,
TimeoutSeconds: timeouts.WatchTimeout(timeouts.TwentyMinute),
})

checkFunc := clusters.IsImportedClusterReady
Expand Down
2 changes: 0 additions & 2 deletions clients/rancher/catalog/clusterrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
)

const (
ClusterRepoSteveResourceType = "catalog.cattle.io.clusterrepo"

action = "action"
chartsURL = "v1/catalog.cattle.io.clusterrepos/"
link = "link"
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, isLeaderMigration)
chartInstallAction := awsChartInstallAction(awsChartInstallActionPayload, repoName, namespaces.KubeSystem, installOptions.ProjectID, isLeaderMigration)

catalogClient, err := client.GetClusterCatalogClient(installOptions.Cluster.ID)
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 @@ -240,7 +238,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 @@ -250,7 +248,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 @@ -263,6 +261,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
}
28 changes: 14 additions & 14 deletions extensions/charts/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ 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"
"github.com/rancher/shepherd/extensions/kubeapi/workloads/daemonsets"
"github.com/rancher/shepherd/extensions/kubeapi/workloads/deployments"
"github.com/rancher/shepherd/extensions/defaults/schema/groupversionresources"
"github.com/rancher/shepherd/extensions/defaults/states"
"github.com/rancher/shepherd/extensions/defaults/stevetypes"
"github.com/rancher/shepherd/extensions/defaults/timeouts"
"github.com/rancher/shepherd/pkg/api/scheme"
"github.com/rancher/shepherd/pkg/wait"
appv1 "k8s.io/api/apps/v1"
Expand All @@ -26,7 +27,6 @@ const (
// serverURLSettingID is a private constant string that contains the ID of server URL setting.
serverURLSettingID = "server-url"
rancherChartsName = "rancher-charts"
active = "active"
)

// InstallOptions is a struct of the required options to install a chart.
Expand Down Expand Up @@ -134,7 +134,7 @@ func WatchAndWaitDeployments(client *rancher.Client, clusterID, namespace string
if err != nil {
return err
}
adminDeploymentResource := adminDynamicClient.Resource(deployments.DeploymentGroupVersionResource).Namespace(namespace)
adminDeploymentResource := adminDynamicClient.Resource(groupversionresources.Deployment()).Namespace(namespace)

deployments, err := adminDeploymentResource.List(context.TODO(), listOptions)
if err != nil {
Expand All @@ -156,7 +156,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.WatchTimeout(timeouts.ThirtyMinute),
})
if err != nil {
return err
Expand Down Expand Up @@ -192,11 +192,11 @@ func WatchAndWaitDeploymentForAnnotation(client *rancher.Client, clusterID, name
if err != nil {
return err
}
adminDeploymentResource := adminDynamicClient.Resource(deployments.DeploymentGroupVersionResource).Namespace(namespace)
adminDeploymentResource := adminDynamicClient.Resource(groupversionresources.Deployment()).Namespace(namespace)

watchAppInterface, err := adminDeploymentResource.Watch(context.TODO(), metav1.ListOptions{
FieldSelector: "metadata.name=" + deploymentName,
TimeoutSeconds: &defaults.WatchTimeoutSeconds,
TimeoutSeconds: timeouts.WatchTimeout(timeouts.ThirtyMinute),
})
if err != nil {
return err
Expand Down Expand Up @@ -234,7 +234,7 @@ func WatchAndWaitDaemonSets(client *rancher.Client, clusterID, namespace string,
if err != nil {
return err
}
adminDaemonSetResource := adminDynamicClient.Resource(daemonsets.DaemonSetGroupVersionResource).Namespace(namespace)
adminDaemonSetResource := adminDynamicClient.Resource(groupversionresources.Daemonset()).Namespace(namespace)

daemonSets, err := adminDaemonSetResource.List(context.TODO(), listOptions)
if err != nil {
Expand All @@ -256,7 +256,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.WatchTimeout(timeouts.ThirtyMinute),
})
if err != nil {
return err
Expand Down Expand Up @@ -314,7 +314,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.WatchTimeout(timeouts.ThirtyMinute),
})
if err != nil {
return err
Expand Down Expand Up @@ -351,20 +351,20 @@ 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
}

for _, repo := range res.Data {
if repo.Name == repoName {
if repo.State.Name == active {
if repo.State.Name == states.Active {
return true, nil
}
}
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
Loading