Skip to content

Commit

Permalink
feat: add instructions to wait harvester-cluster-repo deployment
Browse files Browse the repository at this point in the history
Signed-off-by: PoAn Yang <[email protected]>
  • Loading branch information
FrankYang0529 committed Feb 6, 2025
1 parent 3ca940b commit ba1e141
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/plan/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ func (p *plan) addInstructions(cfg *config.Config, dataDir string) error {
}
}

// If clusterrepo check fails, it waits 5 minutes and retries.
// Install harvester-cluster-repo deployment before clusterrepo,
// so we can avoid the 5 minutes waiting time.
if err := p.addInstruction(resources.ToHarvesterClusterRepoInstruction(cfg.RancherInstallerImage, cfg.SystemDefaultRegistry, k8sVersion, dataDir)); err != nil {
return err
}

if err := p.addInstruction(resources.ToWaitHarvesterClusterRepoInstruction(k8sVersion)); err != nil {
return err
}

if err := p.addInstruction(resources.ToInstruction(cfg.RancherInstallerImage, cfg.SystemDefaultRegistry, k8sVersion, dataDir)); err != nil {
return err
}
Expand Down Expand Up @@ -225,6 +236,11 @@ func (p *plan) addFiles(cfg *config.Config, dataDir string) error {
return err
}

// harvester-cluster-repo manifests
if err := p.addFile(resources.ToHarvesterClusterRepoFile(resources.GetHarvesterClusterRepoManifests(dataDir))); err != nil {
return err
}

// bootstrap manifests
if err := p.addFile(resources.ToBootstrapFile(cfg, resources.GetBootstrapManifests(dataDir))); err != nil {
return err
Expand Down
62 changes: 62 additions & 0 deletions pkg/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (

v1 "github.com/rancher/rancher/pkg/apis/rke.cattle.io/v1"
"github.com/rancher/system-agent/pkg/applyinator"
"github.com/rancher/wrangler/pkg/data/convert"
"github.com/rancher/wrangler/pkg/randomtoken"
"github.com/rancher/wrangler/pkg/yaml"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"

Expand Down Expand Up @@ -153,6 +155,32 @@ func ToBootstrapFile(config *config.Config, path string) (*applyinator.File, err
},
}), path)
}

func ToHarvesterClusterRepoFile(path string) (*applyinator.File, error) {
file := "/usr/share/rancher/rancherd/config.yaml.d/91-harvester-bootstrap-repo.yaml"
bytes, err := os.ReadFile(file)
if err != nil && !os.IsNotExist(err) {
return nil, err
}

logrus.Infof("Loading config file [%s]", file)
values := map[string]interface{}{}
if err := yaml.Unmarshal(bytes, &values); err != nil {
return nil, err
}

result := config.Config{}
convert.ToObj(values, &result)

resources := []v1.GenericMap{}
for _, resource := range result.Resources {
if resource.Data["kind"] == "Deployment" || resource.Data["kind"] == "Service" {
resources = append(resources, resource)
}
}
return ToFile(resources, path)
}

func ToFile(resources []v1.GenericMap, path string) (*applyinator.File, error) {
if len(resources) == 0 {
return nil, nil
Expand Down Expand Up @@ -195,3 +223,37 @@ func ToInstruction(imageOverride, systemDefaultRegistry, k8sVersion, dataDir str
Env: kubectl.Env(k8sVersion),
}, nil
}

func GetHarvesterClusterRepoManifests(dataDir string) string {
return fmt.Sprintf("%s/bootstrapmanifests/harvester-cluster-repo.yaml", dataDir)
}

func ToHarvesterClusterRepoInstruction(imageOverride, systemDefaultRegistry, k8sVersion, dataDir string) (*applyinator.Instruction, error) {
bootstrap := GetHarvesterClusterRepoManifests(dataDir)
cmd, err := self.Self()
if err != nil {
return nil, fmt.Errorf("resolving location of %s: %w", os.Args[0], err)
}
return &applyinator.Instruction{
Name: "harvester-cluster-repo",
SaveOutput: true,
Image: images.GetInstallerImage(imageOverride, systemDefaultRegistry, k8sVersion),
Args: []string{"retry", kubectl.Command(k8sVersion), "apply", "--validate=false", "-f", bootstrap},
Command: cmd,
Env: kubectl.Env(k8sVersion),
}, nil
}

func ToWaitHarvesterClusterRepoInstruction(k8sVersion string) (*applyinator.Instruction, error) {
cmd, err := self.Self()
if err != nil {
return nil, fmt.Errorf("resolving location of %s: %w", os.Args[0], err)
}
return &applyinator.Instruction{
Name: "wait-harvester-cluster-repo",
SaveOutput: true,
Args: []string{"retry", kubectl.Command(k8sVersion), "-n", "cattle-system", "rollout", "status", "-w", "deploy/harvester-cluster-repo"},
Env: kubectl.Env(k8sVersion),
Command: cmd,
}, nil
}

0 comments on commit ba1e141

Please sign in to comment.