package kustomize import ( "fmt" "github.com/bazelbuild/bazel-gazelle/config" "github.com/bazelbuild/bazel-gazelle/label" "github.com/bazelbuild/bazel-gazelle/repo" "github.com/bazelbuild/bazel-gazelle/resolve" "github.com/bazelbuild/bazel-gazelle/rule" container "edge-infra.dev/hack/build/rules/container/gazelle/language" ) // Imports returns a list of ImportSpecs that can be used to import the rule // r. This is used to populate RuleIndex. // // If nil is returned, the rule will not be indexed. If any non-nil slice is // returned, including an empty slice, the rule will be indexed. // // Kustomizations refer to each other by relative path, and only allow one // Kustomization per directory. This means that each Kustomization has a unique // path relative to the repository root, which can be normalized from any other // package. func (k *Kustomize) Imports(_ *config.Config, _ *rule.Rule, f *rule.File) []resolve.ImportSpec { return []resolve.ImportSpec{{ Lang: kustomizationLangName, Imp: f.Pkg, }} } func (k *Kustomize) Resolve(c *config.Config, ix *resolve.RuleIndex, _ *repo.RemoteCache, r *rule.Rule, imports interface{}, _ label.Label) { specs, ok := imports.([]resolve.ImportSpec) if !ok { fmt.Println("failed to assert type ImportSpec from imports") return } else if len(specs) == 0 { return } // Instantiate map for images attr after matching images := make(map[string]string) // TODO(aj185259): When creating these import specs, it'll probably be good // to check the spec.Lang. If it's a `kustomization`, it can follow the below rules // like label.New() being called with a blank repo. Other rules might require // whatever the repo is that's passed. depStrings := []string{} for _, spec := range specs { matches := ix.FindRulesByImportWithConfig(c, spec, spec.Lang) switch len(matches) { case 0: fmt.Printf("no Rules matching %s were found\n", spec.Imp) fmt.Printf("consider running 'bazel run %s %s' to resolve\n", gazellePath, spec.Imp) continue case 1: m := matches[0] rel := label.New("", m.Label.Pkg, m.Label.Name) // If match is not a container_push rule, it should be added to deps // TODO(wc185097_ncrvoyix): this should technically be handled by cross resolve switch spec.Lang { case container.ContainerLangName: images[rel.String()] = spec.Imp continue case kustomizationLangName: depStrings = append(depStrings, rel.String()) continue } default: fmt.Println("somehow more than 1") } // TODO(wc185097_ncrvoyix): this is probably where we will need to handle naming conflicts if len(matches) > 1 { fmt.Printf("multiple possible matches for %s, ignoring", spec.Imp) continue } } if len(images) > 0 { r.SetAttr(imgsAttr, images) } r.DelAttr("deps") r.SetAttr("deps", depStrings) }