Kostiantyn Kalynovskyi c1cce8f9c5 Add single source of Seletors
Also this commit, adds condition to setup testbundle, that ignores
directories

Commit adds new set of functions that allow easy selection of
documents from bundle for different modules.

Relates-To: #61
Closes: #61

Change-Id: I6011203a1f573cbb847e9f57c04aa60bf8278ef1
2020-03-13 16:31:12 -05:00

96 lines
2.5 KiB
Go

package initinfra
import (
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/k8s/client"
)
// Infra is an abstraction used to initialize base infrastructure
type Infra struct {
FileSystem document.FileSystem
RootSettings *environment.AirshipCTLSettings
Client client.Interface
DryRun bool
Prune bool
ClusterType string
}
// NewInfra return instance of Infra
func NewInfra(rs *environment.AirshipCTLSettings) *Infra {
// At this point AirshipCTLSettings may not be fully initialized
infra := &Infra{RootSettings: rs}
return infra
}
// Run intinfra subcommand logic
func (infra *Infra) Run() error {
infra.FileSystem = document.NewDocumentFs()
var err error
infra.Client, err = client.NewClient(infra.RootSettings)
if err != nil {
return err
}
return infra.Deploy()
}
// Deploy method deploys documents
func (infra *Infra) Deploy() error {
kctl := infra.Client.Kubectl()
ao, err := kctl.ApplyOptions()
if err != nil {
return err
}
ao.SetDryRun(infra.DryRun)
// If prune is true, set selector for purning
if infra.Prune {
ao.SetPrune(document.DeployedByLabel + "=" + document.InitinfraIdentifier)
}
globalConf := infra.RootSettings.Config()
if err = globalConf.EnsureComplete(); err != nil {
return err
}
kustomizePath, err := globalConf.CurrentContextEntryPoint(infra.ClusterType, config.Initinfra)
if err != nil {
return err
}
b, err := document.NewBundleByPath(kustomizePath)
if err != nil {
return err
}
selector := document.NewInintInfraSelector()
// TODO (kkalynovskyi) Add Selector that would filter by label indicating wether to deploy it to k8s
docs, err := b.Select(selector)
if err != nil {
return err
}
if len(docs) == 0 {
return document.ErrDocNotFound{}
}
// Label every document indicating that it was deployed by initinfra module for further reference
// This may be used later to get all resources that are part of initinfra module, for monitoring, alerting
// upgrading etc...
// also if prune is set to true, this fulfills requirement for all labeled document to be labeled.
// Pruning by annotation is not available, therefore we need to use label.
for _, doc := range docs {
res := doc.GetKustomizeResource()
labels := res.GetLabels()
labels[document.DeployedByLabel] = document.InitinfraIdentifier
res.SetLabels(labels)
err := doc.SetKustomizeResource(&res)
if err != nil {
return err
}
}
return kctl.Apply(docs, ao)
}