Skip to content

Commit 13285fa

Browse files
Merge pull request #46 from FrankYang0529/HARV-7320
feat: patch provisioning cluster before installing resources
2 parents fd9194c + ba1e141 commit 13285fa

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

pkg/plan/bootstrap.go

+22-5
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,29 @@ func (p *plan) addInstructions(cfg *config.Config, dataDir string) error {
139139
}
140140
}
141141

142-
if err := p.addInstruction(resources.ToInstruction(cfg.RancherInstallerImage, cfg.SystemDefaultRegistry, k8sVersion, dataDir)); err != nil {
143-
return err
144-
}
145-
146-
// currently instruction is needed for version above v2.8.x
142+
// Patch local provisioning cluster status before installing bootstrap resources,
143+
// so bundles can be created first and managed charts can be installed smoothly.
147144
if semver.Compare(cfg.RancherVersion, "v2.8.0") >= 0 {
148145
if err := p.addInstruction(rancher.PatchLocalProvisioningClusterStatus(cfg.RancherInstallerImage, cfg.SystemDefaultRegistry, k8sVersion)); err != nil {
149146
return err
150147
}
151148
}
152149

150+
// If clusterrepo check fails, it waits 5 minutes and retries.
151+
// Install harvester-cluster-repo deployment before clusterrepo,
152+
// so we can avoid the 5 minutes waiting time.
153+
if err := p.addInstruction(resources.ToHarvesterClusterRepoInstruction(cfg.RancherInstallerImage, cfg.SystemDefaultRegistry, k8sVersion, dataDir)); err != nil {
154+
return err
155+
}
156+
157+
if err := p.addInstruction(resources.ToWaitHarvesterClusterRepoInstruction(k8sVersion)); err != nil {
158+
return err
159+
}
160+
161+
if err := p.addInstruction(resources.ToInstruction(cfg.RancherInstallerImage, cfg.SystemDefaultRegistry, k8sVersion, dataDir)); err != nil {
162+
return err
163+
}
164+
153165
if semver.Compare(cfg.RancherVersion, "v2.9.0") >= 0 {
154166
if err := p.addInstruction(rancher.ToRestartRancherWebhookInstruction(k8sVersion)); err != nil {
155167
return err
@@ -224,6 +236,11 @@ func (p *plan) addFiles(cfg *config.Config, dataDir string) error {
224236
return err
225237
}
226238

239+
// harvester-cluster-repo manifests
240+
if err := p.addFile(resources.ToHarvesterClusterRepoFile(resources.GetHarvesterClusterRepoManifests(dataDir))); err != nil {
241+
return err
242+
}
243+
227244
// bootstrap manifests
228245
if err := p.addFile(resources.ToBootstrapFile(cfg, resources.GetBootstrapManifests(dataDir))); err != nil {
229246
return err

pkg/resources/resources.go

+62
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99

1010
v1 "github.com/rancher/rancher/pkg/apis/rke.cattle.io/v1"
1111
"github.com/rancher/system-agent/pkg/applyinator"
12+
"github.com/rancher/wrangler/pkg/data/convert"
1213
"github.com/rancher/wrangler/pkg/randomtoken"
1314
"github.com/rancher/wrangler/pkg/yaml"
15+
"github.com/sirupsen/logrus"
1416
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1517
"k8s.io/apimachinery/pkg/runtime"
1618

@@ -153,6 +155,32 @@ func ToBootstrapFile(config *config.Config, path string) (*applyinator.File, err
153155
},
154156
}), path)
155157
}
158+
159+
func ToHarvesterClusterRepoFile(path string) (*applyinator.File, error) {
160+
file := "/usr/share/rancher/rancherd/config.yaml.d/91-harvester-bootstrap-repo.yaml"
161+
bytes, err := os.ReadFile(file)
162+
if err != nil && !os.IsNotExist(err) {
163+
return nil, err
164+
}
165+
166+
logrus.Infof("Loading config file [%s]", file)
167+
values := map[string]interface{}{}
168+
if err := yaml.Unmarshal(bytes, &values); err != nil {
169+
return nil, err
170+
}
171+
172+
result := config.Config{}
173+
convert.ToObj(values, &result)
174+
175+
resources := []v1.GenericMap{}
176+
for _, resource := range result.Resources {
177+
if resource.Data["kind"] == "Deployment" || resource.Data["kind"] == "Service" {
178+
resources = append(resources, resource)
179+
}
180+
}
181+
return ToFile(resources, path)
182+
}
183+
156184
func ToFile(resources []v1.GenericMap, path string) (*applyinator.File, error) {
157185
if len(resources) == 0 {
158186
return nil, nil
@@ -195,3 +223,37 @@ func ToInstruction(imageOverride, systemDefaultRegistry, k8sVersion, dataDir str
195223
Env: kubectl.Env(k8sVersion),
196224
}, nil
197225
}
226+
227+
func GetHarvesterClusterRepoManifests(dataDir string) string {
228+
return fmt.Sprintf("%s/bootstrapmanifests/harvester-cluster-repo.yaml", dataDir)
229+
}
230+
231+
func ToHarvesterClusterRepoInstruction(imageOverride, systemDefaultRegistry, k8sVersion, dataDir string) (*applyinator.Instruction, error) {
232+
bootstrap := GetHarvesterClusterRepoManifests(dataDir)
233+
cmd, err := self.Self()
234+
if err != nil {
235+
return nil, fmt.Errorf("resolving location of %s: %w", os.Args[0], err)
236+
}
237+
return &applyinator.Instruction{
238+
Name: "harvester-cluster-repo",
239+
SaveOutput: true,
240+
Image: images.GetInstallerImage(imageOverride, systemDefaultRegistry, k8sVersion),
241+
Args: []string{"retry", kubectl.Command(k8sVersion), "apply", "--validate=false", "-f", bootstrap},
242+
Command: cmd,
243+
Env: kubectl.Env(k8sVersion),
244+
}, nil
245+
}
246+
247+
func ToWaitHarvesterClusterRepoInstruction(k8sVersion string) (*applyinator.Instruction, error) {
248+
cmd, err := self.Self()
249+
if err != nil {
250+
return nil, fmt.Errorf("resolving location of %s: %w", os.Args[0], err)
251+
}
252+
return &applyinator.Instruction{
253+
Name: "wait-harvester-cluster-repo",
254+
SaveOutput: true,
255+
Args: []string{"retry", kubectl.Command(k8sVersion), "-n", "cattle-system", "rollout", "status", "-w", "deploy/harvester-cluster-repo"},
256+
Env: kubectl.Env(k8sVersion),
257+
Command: cmd,
258+
}, nil
259+
}

0 commit comments

Comments
 (0)