Skip to content

Commit

Permalink
chore: replace []byte with string and use go:embed for templates
Browse files Browse the repository at this point in the history
Optimize code a bit.

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Apr 10, 2024
1 parent ba7cdc8 commit 5390ccd
Show file tree
Hide file tree
Showing 16 changed files with 574 additions and 550 deletions.
8 changes: 4 additions & 4 deletions internal/app/machined/pkg/controllers/k8s/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (ctrl *ManifestController) Run(ctx context.Context, r controller.Runtime, l
func(r *k8s.Manifest) error {
return k8sadapter.Manifest(r).SetYAML(renderedManifest.data)
}); err != nil {
return fmt.Errorf("error updating manifests: %w", err)
return fmt.Errorf("error updating manifest %q: %w", renderedManifest.name, err)
}
}

Expand All @@ -122,7 +122,7 @@ func (ctrl *ManifestController) Run(ctx context.Context, r controller.Runtime, l
return fmt.Errorf("error listing manifests: %w", err)
}

manifestsToDelete := map[string]struct{}{}
manifestsToDelete := make(map[string]struct{}, len(manifests.Items))

for _, manifest := range manifests.Items {
if manifest.Metadata().Owner() != ctrl.Name() {
Expand Down Expand Up @@ -192,7 +192,7 @@ func (ctrl *ManifestController) render(cfg k8s.BootstrapManifestsConfigSpec, scr

type manifestDesc struct {
name string
template []byte
template string
}

defaultManifests := []manifestDesc{
Expand Down Expand Up @@ -252,7 +252,7 @@ func (ctrl *ManifestController) render(cfg k8s.BootstrapManifestsConfigSpec, scr
"join": strings.Join,
"contains": strings.Contains,
}).
Parse(string(defaultManifests[i].template))
Parse(defaultManifests[i].template)
if err != nil {
return nil, fmt.Errorf("error parsing manifest template %q: %w", defaultManifests[i].name, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (ctrl *RenderSecretsStaticPodController) Run(ctx context.Context, r control

type template struct {
filename string
template []byte
template string
}

for _, pod := range []struct {
Expand Down Expand Up @@ -253,7 +253,7 @@ func (ctrl *RenderSecretsStaticPodController) Run(ctx context.Context, r control
templates: []template{
{
filename: "kubeconfig",
template: []byte("{{ .Secrets.ControllerManagerKubeconfig }}"),
template: "{{ .Secrets.ControllerManagerKubeconfig }}",
},
},
},
Expand All @@ -265,7 +265,7 @@ func (ctrl *RenderSecretsStaticPodController) Run(ctx context.Context, r control
templates: []template{
{
filename: "kubeconfig",
template: []byte("{{ .Secrets.SchedulerKubeconfig }}"),
template: "{{ .Secrets.SchedulerKubeconfig }}",
},
},
},
Expand Down Expand Up @@ -311,7 +311,7 @@ func (ctrl *RenderSecretsStaticPodController) Run(ctx context.Context, r control
for _, templ := range pod.templates {
var t *stdlibtemplate.Template

t, err = stdlibtemplate.New(templ.filename).Parse(string(templ.template))
t, err = stdlibtemplate.New(templ.filename).Parse(templ.template)
if err != nil {
return fmt.Errorf("error parsing template %q: %w", templ.filename, err)
}
Expand Down
Loading

0 comments on commit 5390ccd

Please sign in to comment.