Merge "Change document labeling"

This commit is contained in:
Zuul 2020-02-20 15:22:40 +00:00 committed by Gerrit Code Review
commit 94deadd7b1
10 changed files with 66 additions and 47 deletions

View File

@ -43,11 +43,11 @@ func getDataFromSecret(cfg document.Document, key string) ([]byte, error) {
} }
// GetCloudData reads YAML document input and generates cloud-init data for // GetCloudData reads YAML document input and generates cloud-init data for
// node (i.e. Cluster API Machine) with bootstrap annotation. // node (i.e. Cluster API Machine) with bootstrap label.
func GetCloudData(docBundle document.Bundle, bsAnnotation string) ([]byte, []byte, error) { func GetCloudData(docBundle document.Bundle, bsSelector string) ([]byte, []byte, error) {
var userData []byte var userData []byte
var netConf []byte var netConf []byte
docs, err := docBundle.GetByAnnotation(bsAnnotation) docs, err := docBundle.GetByLabel(bsSelector)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -60,7 +60,7 @@ func GetCloudData(docBundle document.Bundle, bsAnnotation string) ([]byte, []byt
} }
if ephemeralCfg == nil { if ephemeralCfg == nil {
return nil, nil, document.ErrDocNotFound{ return nil, nil, document.ErrDocNotFound{
Annotation: bsAnnotation, Selector: bsSelector,
Kind: EphemeralClusterConfKind, Kind: EphemeralClusterConfKind,
} }
} }

View File

@ -16,22 +16,22 @@ func TestGetCloudData(t *testing.T) {
require.NoError(t, err, "Building Bundle Failed") require.NoError(t, err, "Building Bundle Failed")
tests := []struct { tests := []struct {
ann string selector string
expectedUserData []byte expectedUserData []byte
expectedNetData []byte expectedNetData []byte
expectedErr error expectedErr error
}{ }{
{ {
ann: "test=test", selector: "test=test",
expectedUserData: nil, expectedUserData: nil,
expectedNetData: nil, expectedNetData: nil,
expectedErr: document.ErrDocNotFound{ expectedErr: document.ErrDocNotFound{
Annotation: "test=test", Selector: "test=test",
Kind: "Secret", Kind: "Secret",
}, },
}, },
{ {
ann: "airshipit.org/clustertype=nodata", selector: "airshipit.org/ephemeral=false",
expectedUserData: nil, expectedUserData: nil,
expectedNetData: nil, expectedNetData: nil,
expectedErr: ErrDataNotSupplied{ expectedErr: ErrDataNotSupplied{
@ -40,7 +40,7 @@ func TestGetCloudData(t *testing.T) {
}, },
}, },
{ {
ann: "test=nodataforcfg", selector: "test=nodataforcfg",
expectedUserData: nil, expectedUserData: nil,
expectedNetData: nil, expectedNetData: nil,
expectedErr: ErrDataNotSupplied{ expectedErr: ErrDataNotSupplied{
@ -49,7 +49,13 @@ func TestGetCloudData(t *testing.T) {
}, },
}, },
{ {
ann: "airshipit.org/clustertype=ephemeral", selector: "airshipit.org/ephemeral=true",
expectedUserData: []byte("cloud-init"),
expectedNetData: []byte("netconfig\n"),
expectedErr: nil,
},
{
selector: "some-data in (true, True)",
expectedUserData: []byte("cloud-init"), expectedUserData: []byte("cloud-init"),
expectedNetData: []byte("netconfig\n"), expectedNetData: []byte("netconfig\n"),
expectedErr: nil, expectedErr: nil,
@ -57,7 +63,7 @@ func TestGetCloudData(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
actualUserData, actualNetData, actualErr := GetCloudData(bundle, tt.ann) actualUserData, actualNetData, actualErr := GetCloudData(bundle, tt.selector)
assert.Equal(t, tt.expectedUserData, actualUserData) assert.Equal(t, tt.expectedUserData, actualUserData)
assert.Equal(t, tt.expectedNetData, actualNetData) assert.Equal(t, tt.expectedNetData, actualNetData)

View File

@ -1,8 +1,8 @@
apiVersion: v1 apiVersion: v1
kind: Secret kind: Secret
metadata: metadata:
annotations: labels:
airshipit.org/clustertype: ephemeral airshipit.org/ephemeral: "true"
name: node1-bmc-secret name: node1-bmc-secret
type: Opaque type: Opaque
data: data:
@ -13,17 +13,29 @@ stringData:
apiVersion: v1 apiVersion: v1
kind: Secret kind: Secret
metadata: metadata:
annotations: labels:
airshipit.org/clustertype: nodata airshipit.org/ephemeral: "false"
name: node1-bmc-secret1 name: node1-bmc-secret1
type: Opaque type: Opaque
--- ---
apiVersion: v1 apiVersion: v1
kind: Secret kind: Secret
metadata: metadata:
annotations: labels:
test: nodataforcfg test: nodataforcfg
name: node1-bmc-secret2 name: node1-bmc-secret2
type: Opaque type: Opaque
data: data:
foo: bmV0Y29uZmlnCg== foo: bmV0Y29uZmlnCg==
---
apiVersion: v1
kind: Secret
metadata:
labels:
some-data: "True"
name: node1-bmc-in-secret2
type: Opaque
data:
netconfig: bmV0Y29uZmlnCg==
stringData:
userdata: cloud-init

View File

@ -118,7 +118,8 @@ func generateBootstrapIso(
) error { ) error {
cntVol := strings.Split(cfg.Container.Volume, ":")[1] cntVol := strings.Split(cfg.Container.Volume, ":")[1]
log.Print("Creating cloud-init for ephemeral K8s") log.Print("Creating cloud-init for ephemeral K8s")
userData, netConf, err := cloudinit.GetCloudData(docBundle, document.EphemeralClusterMarker) label := document.EphemeralClusterSelector
userData, netConf, err := cloudinit.GetCloudData(docBundle, label)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,8 +1,8 @@
apiVersion: v1 apiVersion: v1
kind: Secret kind: Secret
metadata: metadata:
annotations: labels:
airshipit.org/clustertype: ephemeral airshipit.org/ephemeral: "true"
name: node1-bmc-secret name: node1-bmc-secret
type: Opaque type: Opaque
data: data:

View File

@ -2,8 +2,7 @@ package document
// Document labels and annotations // Document labels and annotations
const ( const (
ClusterType = "clustertype" BaseAirshipSelector = "airshipit.org"
// TODO (dukov) Replace with constants defined in config module once EphemeralClusterSelector = BaseAirshipSelector + "/ephemeral in (True, true)"
// module dependency loop has been resolved TargetClusterSelector = BaseAirshipSelector + "/target in (True, true)"
EphemeralClusterMarker = "airshipit.org/clustertype=ephemeral"
) )

View File

@ -6,10 +6,10 @@ import (
// ErrDocNotFound returned if desired document not found // ErrDocNotFound returned if desired document not found
type ErrDocNotFound struct { type ErrDocNotFound struct {
Annotation string Selector string
Kind string Kind string
} }
func (e ErrDocNotFound) Error() string { func (e ErrDocNotFound) Error() string {
return fmt.Sprintf("Document annotated by %s with Kind %s not found", e.Annotation, e.Kind) return fmt.Sprintf("Document filtered by selector %s with Kind %s not found", e.Selector, e.Kind)
} }

View File

@ -84,9 +84,10 @@ func getRemoteDirectConfig(settings *environment.AirshipCTLSettings) (*config.Re
return nil, "", err return nil, "", err
} }
label := document.EphemeralClusterSelector
filter := types.Selector{ filter := types.Selector{
Gvk: gvk.FromKind(AirshipHostKind), Gvk: gvk.FromKind(AirshipHostKind),
LabelSelector: document.EphemeralClusterMarker, LabelSelector: label,
} }
docs, err := docBundle.Select(filter) docs, err := docBundle.Select(filter)
if err != nil { if err != nil {
@ -94,7 +95,7 @@ func getRemoteDirectConfig(settings *environment.AirshipCTLSettings) (*config.Re
} }
if len(docs) == 0 { if len(docs) == 0 {
return nil, "", document.ErrDocNotFound{ return nil, "", document.ErrDocNotFound{
Annotation: document.EphemeralClusterMarker, Selector: label,
Kind: AirshipHostKind, Kind: AirshipHostKind,
} }
} }

View File

@ -3,7 +3,7 @@ apiVersion: metal3.io/v1alpha1
kind: BareMetalHost kind: BareMetalHost
metadata: metadata:
labels: labels:
airshipit.org/clustertype: ephemeral airshipit.org/ephemeral: "true"
name: master-0 name: master-0
spec: spec:
online: true online: true
@ -15,8 +15,8 @@ spec:
apiVersion: v1 apiVersion: v1
kind: Secret kind: Secret
metadata: metadata:
annotations: labels:
airshipit.org/clustertype: ephemeral airshipit.org/ephemeral: "true"
name: master-0-bmc-secret name: master-0-bmc-secret
type: Opaque type: Opaque
data: data:
@ -26,11 +26,11 @@ data:
apiVersion: metal3.io/v1alpha1 apiVersion: metal3.io/v1alpha1
kind: BareMetalHost kind: BareMetalHost
metadata: metadata:
annotations: labels:
airshipit.org/clustertype: target airshipit.org/target: "true"
name: master-1 name: master-1
spec: spec:
online: true online: "true"
bootMACAddress: 01:3b:8b:0c:ec:8b bootMACAddress: 01:3b:8b:0c:ec:8b
bmc: bmc:
address: ipmi://192.168.111.2:6230 address: ipmi://192.168.111.2:6230
@ -39,8 +39,8 @@ spec:
apiVersion: v1 apiVersion: v1
kind: Secret kind: Secret
metadata: metadata:
annotations: labels:
airshipit.org/clustertype: target airshipit.org/target: "true"
name: master-1-bmc-secret name: master-1-bmc-secret
type: Opaque type: Opaque
data: data:

View File

@ -3,7 +3,7 @@ apiVersion: metal3.io/v1alpha1
kind: BareMetalHost kind: BareMetalHost
metadata: metadata:
labels: labels:
airshipit.org/clustertype: ephemeral airshipit.org/ephemeral: "true"
name: master-0 name: master-0
spec: spec:
online: true online: true
@ -15,8 +15,8 @@ spec:
apiVersion: v1 apiVersion: v1
kind: Secret kind: Secret
metadata: metadata:
annotations: labels:
airshipit.org/clustertype: ephemeral airshipit.org/ephemeral: "true"
name: master-0-bmc-secret name: master-0-bmc-secret
type: Opaque type: Opaque
data: data:
@ -26,8 +26,8 @@ data:
apiVersion: metal3.io/v1alpha1 apiVersion: metal3.io/v1alpha1
kind: BareMetalHost kind: BareMetalHost
metadata: metadata:
annotations: labels:
airshipit.org/clustertype: target airshipit.org/target: "true"
name: master-1 name: master-1
spec: spec:
online: true online: true
@ -39,8 +39,8 @@ spec:
apiVersion: v1 apiVersion: v1
kind: Secret kind: Secret
metadata: metadata:
annotations: labels:
airshipit.org/clustertype: target airshipit.org/target: "true"
name: master-1-bmc-secret name: master-1-bmc-secret
type: Opaque type: Opaque
data: data: