airshipctl/pkg/document/selectors.go
Anderson, Craig (ca846m) 2daacf5f2a image-builder integration for ISO builds
This introduces airshipctl integration with image-builder [0], which
replaces the existing isogen tool for ephemeral ISO generation.

The airshipctl isogen executor has been updated for building ephemeral
ISOs using the image-builder container. The ability for user-declared
filenames for cloud-init user data and network data was removed, since
the user's only interest is in supplying the relevant overrides, not in
transparent naming coordination with the image-builder container. A new
object is added to the document package to identify the document kind,
label, and key to retrieve data from since this is pattern we will
reuse elsewhere.

Progress flag removed as requsted. Progress is reported directly by the
image-builder container.

Isogen debug flag removed in favor of using log.DebugEnabled()

[0] https://review.opendev.org/#/c/730777/

Depends-On: https://review.opendev.org/c/airship/images/+/730777/
Change-Id: I545004feaf2116f8ffb29faf6f7f7f5fcfe24fff
2020-12-17 14:47:53 -08:00

226 lines
6.6 KiB
Go

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package document
import (
"fmt"
"strings"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/kustomize/api/resid"
"sigs.k8s.io/kustomize/api/types"
)
// Selector provides abstraction layer in front of kustomize selector
type Selector struct {
types.Selector
}
// NewSelector returns instance of Selector container
func NewSelector() Selector {
return Selector{}
}
// Following set of functions allows to build selector object
// by name, gvk, label selector and annotation selector
// ByName select by name
func (s Selector) ByName(name string) Selector {
s.Name = name
return s
}
// ByNamespace select by namespace
func (s Selector) ByNamespace(namespace string) Selector {
s.Namespace = namespace
return s
}
// ByGvk select by gvk
func (s Selector) ByGvk(group, version, kind string) Selector {
s.Gvk = resid.Gvk{Group: group, Version: version, Kind: kind}
return s
}
// ByKind select by Kind
func (s Selector) ByKind(kind string) Selector {
s.Gvk = resid.Gvk{Kind: kind}
return s
}
// ByLabel select by label selector
func (s Selector) ByLabel(labelSelector string) Selector {
if s.LabelSelector != "" {
s.LabelSelector = strings.Join([]string{s.LabelSelector, labelSelector}, ",")
} else {
s.LabelSelector = labelSelector
}
return s
}
// ByAnnotation select by annotation selector.
func (s Selector) ByAnnotation(annotationSelector string) Selector {
if s.AnnotationSelector != "" {
s.AnnotationSelector = strings.Join([]string{s.AnnotationSelector, annotationSelector}, ",")
} else {
s.AnnotationSelector = annotationSelector
}
return s
}
// ByObject select by runtime object defined in API schema
func (s Selector) ByObject(obj runtime.Object, scheme *runtime.Scheme) (Selector, error) {
gvks, _, err := scheme.ObjectKinds(obj)
if err != nil {
return Selector{}, err
}
if len(gvks) != 1 {
return Selector{}, ErrRuntimeObjectKind{Obj: obj}
}
result := NewSelector().ByGvk(gvks[0].Group, gvks[0].Version, gvks[0].Kind)
accessor, err := meta.Accessor(obj)
if err != nil {
return Selector{}, err
}
if name := accessor.GetName(); name != "" {
result = result.ByName(name)
}
return result, nil
}
// String is a convenience function which dumps all relevant information about a Selector in the following format:
// [Key1=Value1, Key2=Value2, ...]
func (s Selector) String() string {
var components []string
if s.Group != "" {
components = append(components, fmt.Sprintf("Group=%q", s.Group))
}
if s.Version != "" {
components = append(components, fmt.Sprintf("Version=%q", s.Version))
}
if s.Kind != "" {
components = append(components, fmt.Sprintf("Kind=%q", s.Kind))
}
if s.Namespace != "" {
components = append(components, fmt.Sprintf("Namespace=%q", s.Namespace))
}
if s.Name != "" {
components = append(components, fmt.Sprintf("Name=%q", s.Name))
}
if s.AnnotationSelector != "" {
components = append(components, fmt.Sprintf("Annotations=%q", s.AnnotationSelector))
}
if s.LabelSelector != "" {
components = append(components, fmt.Sprintf("Labels=%q", s.LabelSelector))
}
if len(components) == 0 {
return "No selection conditions specified"
}
return fmt.Sprintf("[%s]", strings.Join(components, ", "))
}
// NewEphemeralCloudDataSelector returns selector to get BaremetalHost for ephemeral node
func NewEphemeralCloudDataSelector() Selector {
return NewSelector().ByKind(SecretKind).ByLabel(EphemeralUserDataSelector)
}
// NewEphemeralBMHSelector returns selector to get BaremetalHost for ephemeral node
func NewEphemeralBMHSelector() Selector {
return NewSelector().ByKind(BareMetalHostKind).ByLabel(EphemeralHostSelector)
}
// NewBMCCredentialsSelector returns selector to get BaremetalHost BMC credentials
func NewBMCCredentialsSelector(name string) Selector {
return NewSelector().ByKind(SecretKind).ByName(name)
}
// NewNetworkDataSelector returns selector that can be used to get secret with
// network data bmhDoc argument is a document interface, that should hold fields
// spec.networkData.name and spec.networkData.namespace where to find the secret,
// if either of these fields are not defined in Document error will be returned
func NewNetworkDataSelector(bmhDoc Document) (Selector, error) {
selector := NewSelector()
// extract the network data document pointer from the bmh document
netConfDocName, err := bmhDoc.GetString("spec.networkData.name")
if err != nil {
return selector, err
}
netConfDocNamespace, err := bmhDoc.GetString("spec.networkData.namespace")
if err != nil {
return selector, err
}
// try and find these documents in our bundle
selector = selector.
ByKind(SecretKind).
ByNamespace(netConfDocNamespace).
ByName(netConfDocName)
return selector, nil
}
// NewDeployToK8sSelector returns a selector to get documents that are to be deployed
// to kubernetes cluster.
func NewDeployToK8sSelector() Selector {
return NewSelector().ByLabel(DeployToK8sSelector)
}
// NewClusterctlMetadataSelector returns selector to get clusterctl metadata documents
func NewClusterctlMetadataSelector() Selector {
return NewSelector().ByGvk(ClusterctlMetadataGroup,
ClusterctlMetadataVersion,
ClusterctlMetadataKind)
}
//GetSecretData returns data located with a given key of a given document
func GetSecretData(docBundle Bundle, apiSelector types.Selector, key string) ([]byte, error) {
s := NewSelector()
if apiSelector.Group != "" && apiSelector.Version != "" && apiSelector.Kind != "" {
s = s.ByGvk(apiSelector.Group, apiSelector.Version, apiSelector.Kind)
} else if apiSelector.Kind != "" {
s = s.ByKind(apiSelector.Kind)
}
if apiSelector.Namespace != "" {
s = s.ByNamespace(apiSelector.Namespace)
}
if apiSelector.Name != "" {
s = s.ByName(apiSelector.Name)
}
if apiSelector.AnnotationSelector != "" {
s = s.ByAnnotation(apiSelector.AnnotationSelector)
}
if apiSelector.LabelSelector != "" {
s = s.ByLabel(apiSelector.LabelSelector)
}
doc, docErr := docBundle.SelectOne(s)
if docErr != nil {
return nil, docErr
}
// finally, try and retrieve the data we want from the document
data, keyErr := DecodeSecretData(doc, key)
if keyErr != nil {
return nil, keyErr
}
return data, nil
}