Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider using go embed to keep object definitions in YAMLs rather than in Go code #394

Closed
rojkov opened this issue Jun 9, 2020 · 10 comments · Fixed by #747
Closed

Consider using go embed to keep object definitions in YAMLs rather than in Go code #394

rojkov opened this issue Jun 9, 2020 · 10 comments · Fixed by #747
Assignees
Projects

Comments

@rojkov
Copy link
Contributor

rojkov commented Jun 9, 2020

Currently in the operator we construct DaemonSet objects directly as a Go structure:

ds := &apps.DaemonSet{
    ObjectMeta: metav1.ObjectMeta{...},
    Spec: apps.DaemonSetSpec{...},
}

This way we end up with very long though simple functions and this prevents us from enabling some linter checks like funlen.

Evaluate moving object definitions to YAML files and generating Go files with go-bindata the way it's done in OpenShift operators: https://github.com/openshift/cluster-ingress-operator/tree/master/pkg/manifests.

Try to annotate the generated go-bindata files with buildtags so that linter checks don't go crazy: e.g. generate debug and release versions of the Go files and enable linter checks only for the debug version.

@msivosuo msivosuo added this to Next Quarter in Backlog Sep 23, 2020
@msivosuo msivosuo moved this from Next Quarter to To Do in Backlog Nov 27, 2020
@mythi
Copy link
Contributor

mythi commented Feb 18, 2021

@rojkov
Copy link
Contributor Author

rojkov commented Feb 18, 2021

Nice new feature. Looks cleaner than go-bindata

@mythi
Copy link
Contributor

mythi commented Feb 24, 2021

@rojkov I gave it a try using tips from this blog post

package main

import (
	_ "embed"
	"fmt"
	"sigs.k8s.io/yaml"
	apps "k8s.io/api/apps/v1"

)
var (
    //go:embed deployments/qat_plugin/base/intel-qat-plugin.yaml
    b []byte
    s = func() (s apps.DaemonSet) {
	   err := yaml.Unmarshal(b, &s)
	   if err != nil {
		   panic(err)
	   }
	   return
    }()
)

func main() {
    fmt.Printf("s: %#v\n", s)
}

@mythi mythi changed the title Consider using go-bindata to keep object definitions in YAMLs rather than in Go code Consider using go embed to keep object definitions in YAMLs rather than in Go code Jun 17, 2021
@mythi
Copy link
Contributor

mythi commented Jun 17, 2021

Updated the title: Go embed likely better option than go-bindata.

@msivosuo msivosuo moved this from To Do to Next Quarter in Backlog Jun 17, 2021
@msivosuo msivosuo removed this from Next Quarter in Backlog Jun 22, 2021
@msivosuo msivosuo added this to To do in 2021-9 via automation Jun 22, 2021
@msivosuo msivosuo removed this from To do in 2021-9 Jun 22, 2021
@msivosuo msivosuo added this to To do in 2021-8 via automation Jun 22, 2021
@rojkov
Copy link
Contributor Author

rojkov commented Jun 22, 2021

Moving the Go code to YAML files may enable us to add the funlen check to .golangci.yaml.

@msivosuo msivosuo removed this from To do in 2021-8 Aug 18, 2021
@msivosuo msivosuo added this to To do in 2021-9 via automation Aug 18, 2021
@msivosuo msivosuo removed this from To do in 2021-9 Aug 18, 2021
@msivosuo msivosuo added this to To Do in Backlog via automation Aug 18, 2021
@msivosuo msivosuo moved this from To Do to Next Quarter in Backlog Aug 18, 2021
@msivosuo msivosuo added this to To do in 2021-11 via automation Sep 14, 2021
@msivosuo msivosuo removed this from Next Quarter in Backlog Sep 14, 2021
@msivosuo msivosuo assigned bart0sh and unassigned ozhuraki Sep 15, 2021
@bart0sh bart0sh moved this from To do to In progress in 2021-11 Nov 8, 2021
@mythi
Copy link
Contributor

mythi commented Nov 15, 2021

@bart0sh one problem to solve first is how to make embed to find the YAMLs because the embedded files must be under same dir hierarcy (and symlinks are not allowed) as the code that embeds them.

@pohly
Copy link

pohly commented Nov 15, 2021

In PMEM-CSI, we treat our deploy directory as a Go package. That package then embeds the YAML files and exports them with a suitable API.

@bart0sh
Copy link
Member

bart0sh commented Nov 15, 2021

@mythi it worked this way: #747
We can either symlink embed.go for other plugins or just copy this file as it should be identical for all of them.

@mythi
Copy link
Contributor

mythi commented Nov 15, 2021

@bart0sh this looks good to me (and thanks @pohly!)

We can either symlink embed.go for other plugins or just copy this file as it should be identical for all of them.

or have deployments/embed.go that pulls in all devices?

@bart0sh
Copy link
Member

bart0sh commented Nov 15, 2021

@mythi thanks. that's even better as yamls do not always cooperate with that simple wildcard pattern. will do.

2021-11 automation moved this from In progress to Done Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

5 participants