-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rke2 template base commit PR updates More PR updates Update rke2template.go PR updates repositories Update rke2template.go Update rke2template.go Update rke2template.go Update rke2template.go Update rke2template.go config updates Update wait.go Generic Create with polling Update go.mod Update go.mod Update steve.go
- Loading branch information
1 parent
6d1ffee
commit 9010305
Showing
6 changed files
with
224 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package charts | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/rancher/shepherd/clients/rancher" | ||
"github.com/rancher/shepherd/extensions/cloudcredentials" | ||
"github.com/rancher/shepherd/extensions/clusters" | ||
"github.com/rancher/shepherd/extensions/defaults" | ||
"github.com/rancher/shepherd/extensions/projects" | ||
"github.com/rancher/shepherd/extensions/wait" | ||
"github.com/rancher/shepherd/pkg/api/steve/catalog/types" | ||
"github.com/rancher/shepherd/pkg/namegenerator" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
const ( | ||
fleetNamespace = "fleet-default" | ||
localCluster = "local" | ||
) | ||
|
||
// InstallTemplateChart installs a template from a repo. | ||
func InstallTemplateChart(client *rancher.Client, repoName, templateName, clusterName, k8sVersion string, credentials *cloudcredentials.CloudCredential) error { | ||
latestVersion, err := client.Catalog.GetLatestChartVersion(templateName, repoName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
project, err := projects.GetProjectByName(client, localCluster, "System") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
installOptions := &InstallOptions{ | ||
Cluster: &clusters.ClusterMeta{ | ||
ID: localCluster, | ||
}, | ||
Version: latestVersion, | ||
ProjectID: project.ID, | ||
} | ||
|
||
serverSetting, err := client.Management.Setting.ByID(serverURLSettingID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
registrySetting, err := client.Management.Setting.ByID(defaultRegistrySettingID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
chartInstallActionPayload := &payloadOpts{ | ||
InstallOptions: *installOptions, | ||
Name: templateName, | ||
Namespace: fleetNamespace, | ||
Host: serverSetting.Value, | ||
DefaultRegistry: registrySetting.Value, | ||
} | ||
|
||
chartValues, err := client.Catalog.GetChartValues(repoName, templateName, installOptions.Version) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
chartInstallAction := TemplateInstallAction(chartInstallActionPayload, repoName, clusterName, credentials.ID, k8sVersion, fleetNamespace, chartValues) | ||
|
||
catalogClient, err := client.GetClusterCatalogClient(installOptions.Cluster.ID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = client.Catalog.InstallChart(chartInstallAction, repoName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client.Session.RegisterCleanupFunc(func() error { | ||
err := client.Catalog.UninstallChart(templateName, fleetNamespace, newChartUninstallAction()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
watchAppInterface, err := catalogClient.Apps(fleetNamespace).Watch(context.TODO(), metav1.ListOptions{ | ||
FieldSelector: "metadata.name=" + templateName, | ||
TimeoutSeconds: &defaults.WatchTimeoutSeconds, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = wait.ResourceDelete(watchAppInterface) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
err = VerifyChartInstall(catalogClient, fleetNamespace, templateName) | ||
if err != nil { | ||
return err | ||
} | ||
return err | ||
} | ||
|
||
// TemplateInstallAction creates the payload used when installing a template chart | ||
func TemplateInstallAction(InstallActionPayload *payloadOpts, repoName, clusterName, cloudCredential, k8sVersion, namespace string, chartValues map[string]any) *types.ChartInstallAction { | ||
chartValues["cloudCredentialSecretName"] = cloudCredential | ||
chartValues["kubernetesVersion"] = k8sVersion | ||
chartValues["cluster"].(map[string]any)["name"] = clusterName | ||
|
||
for index := range chartValues["nodepools"].(map[string]any) { | ||
chartValues["nodepools"].(map[string]any)[index].(map[string]any)["name"] = namegenerator.AppendRandomString("nodepool") | ||
} | ||
|
||
chartInstall := newChartInstall( | ||
InstallActionPayload.Name, | ||
InstallActionPayload.InstallOptions.Version, | ||
InstallActionPayload.InstallOptions.Cluster.ID, | ||
InstallActionPayload.InstallOptions.Cluster.Name, | ||
InstallActionPayload.Host, | ||
repoName, | ||
InstallActionPayload.InstallOptions.ProjectID, | ||
InstallActionPayload.DefaultRegistry, | ||
chartValues) | ||
chartInstalls := []types.ChartInstall{*chartInstall} | ||
|
||
return newChartInstallAction(namespace, InstallActionPayload.ProjectID, chartInstalls) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package wait | ||
|
||
import ( | ||
"github.com/rancher/shepherd/pkg/wait" | ||
"k8s.io/apimachinery/pkg/watch" | ||
) | ||
|
||
// ResourceCreate is a generic wait function for create operations on v1 resources | ||
func ResourceCreate(watchInterface watch.Interface) error { | ||
err := wait.WatchWait(watchInterface, func(event watch.Event) (ready bool, err error) { | ||
if event.Type == watch.Added { | ||
return true, nil | ||
} else if event.Type == watch.Error { | ||
return false, nil | ||
} | ||
|
||
return false, nil | ||
}) | ||
|
||
return err | ||
|
||
} | ||
|
||
// ResourceCreate is a generic wait function for delete operations on v1 resources | ||
func ResourceDelete(watchInterface watch.Interface) error { | ||
err := wait.WatchWait(watchInterface, func(event watch.Event) (ready bool, err error) { | ||
if event.Type == watch.Error { | ||
return false, nil | ||
} else if event.Type == watch.Deleted { | ||
return true, nil | ||
} | ||
|
||
return false, nil | ||
}) | ||
|
||
return err | ||
} |