Skip to content

Commit

Permalink
include labels and annotations when generating ResourceGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
Liujingfang1 committed Oct 26, 2020
1 parent d9b4cb2 commit 33d99f6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
22 changes: 15 additions & 7 deletions pkg/live/rgpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"strings"

"github.com/GoogleContainerTools/kpt/pkg/kptfile"
"github.com/GoogleContainerTools/kpt/pkg/kptfile/kptfileutil"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/cli-runtime/pkg/resource"
Expand Down Expand Up @@ -38,7 +39,7 @@ func (p *ResourceGroupPathManifestReader) Read() ([]*resource.Info, error) {
}
inv := kf.Inventory
klog.V(4).Infof("generating ResourceGroup inventory object %s/%s/%s", inv.Namespace, inv.Name, inv.InventoryID)
invInfo, err := generateInventoryObj(inv.Name, inv.Namespace, inv.InventoryID)
invInfo, err := generateInventoryObj(inv)
if err != nil {
return []*resource.Info{}, err
}
Expand All @@ -48,22 +49,22 @@ func (p *ResourceGroupPathManifestReader) Read() ([]*resource.Info, error) {

// generateInventoryObj returns the ResourceGroupInventory object using the
// passed information.
func generateInventoryObj(name string, namespace string, id string) (*resource.Info, error) {
func generateInventoryObj(inv kptfile.Inventory) (*resource.Info, error) {
// Validate the parameters
name = strings.TrimSpace(name)
name := strings.TrimSpace(inv.Name)
if name == "" {
return nil, fmt.Errorf("kptfile inventory empty name")
}
namespace = strings.TrimSpace(namespace)
namespace := strings.TrimSpace(inv.Namespace)
if namespace == "" {
return nil, fmt.Errorf("kptfile inventory empty namespace")
}
id = strings.TrimSpace(id)
id := strings.TrimSpace(inv.InventoryID)
if id == "" {
return nil, fmt.Errorf("kptfile inventory missing inventoryID")
}
// Create and return ResourceGroup custom resource as inventory object.
var inventoryObj = unstructured.Unstructured{
var inventoryObj = &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "configmanagement.gke.io/v1beta1",
"kind": "ResourceGroup",
Expand All @@ -79,10 +80,17 @@ func generateInventoryObj(name string, namespace string, id string) (*resource.I
},
},
}
labels := inv.Labels
if labels == nil {
labels = make(map[string]string)
}
labels[common.InventoryLabel] = id
inventoryObj.SetLabels(labels)
inventoryObj.SetAnnotations(inv.Annotations)
var invInfo = &resource.Info{
Namespace: namespace,
Name: name,
Object: &inventoryObj,
Object: inventoryObj,
}
return invInfo, nil
}
48 changes: 48 additions & 0 deletions pkg/live/rgpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ inventory:
namespace: test-namespace
name: inventory-obj-name
`
var kptFileWithAnnotations = `
apiVersion: kpt.dev/v1alpha1
kind: Kptfile
metadata:
name: test1
upstream:
type: git
git:
commit: 786b898857bd7e9647c229d5f39b0be4de86c915
repo: git@github.com:seans3/blueprint-helloworld
directory: /
ref: master
inventory:
namespace: test-namespace
name: inventory-obj-name
inventoryID: XXXXXXXXXX-FOOOOOO
annotations:
random-key: random-value
`

var podA = `
apiVersion: v1
Expand Down Expand Up @@ -86,6 +105,7 @@ func TestInvGenPathManifestReader_Read(t *testing.T) {
testCases := map[string]struct {
manifests map[string]string
numInfos int
annotated bool
isError bool
}{
"Kptfile missing inventory id is error": {
Expand Down Expand Up @@ -121,6 +141,16 @@ func TestInvGenPathManifestReader_Read(t *testing.T) {
numInfos: 2,
isError: false,
},
"ResourceGroup inventory object created with annotation, multiple objects": {
manifests: map[string]string{
"Kptfile": kptFileWithAnnotations,
"pod-a.yaml": podA,
"deployment-a.yaml": deploymentA,
},
numInfos: 3,
annotated: true,
isError: false,
},
}

for tn, tc := range testCases {
Expand Down Expand Up @@ -170,6 +200,12 @@ func TestInvGenPathManifestReader_Read(t *testing.T) {
actualID, err := getInventoryLabel(invInfo)
assert.NoError(t, err)
assert.Equal(t, inventoryID, actualID)
actualAnnotations := getInventoryAnnotations(invInfo)
if tc.annotated {
assert.Equal(t, map[string]string{"random-key": "random-value"}, actualAnnotations)
} else {
assert.Equal(t, map[string]string(nil), actualAnnotations)
}
})
}
}
Expand All @@ -190,3 +226,15 @@ func getInventoryLabel(inv *resource.Info) (string, error) {
}
return inventoryLabel, nil
}

func getInventoryAnnotations(inv *resource.Info) map[string]string {
obj := inv.Object
if obj == nil {
return nil
}
accessor, err := meta.Accessor(obj)
if err != nil {
return nil
}
return accessor.GetAnnotations()
}
2 changes: 1 addition & 1 deletion pkg/live/rgstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ func transformKptfile(resource []byte) (*resource.Info, error) {
}
inv := kptFileTemplate.Inventory
klog.V(4).Infof("generating ResourceGroup inventory object %s/%s/%s", inv.Namespace, inv.Name, inv.InventoryID)
return generateInventoryObj(inv.Name, inv.Namespace, inv.InventoryID)
return generateInventoryObj(inv)
}

0 comments on commit 33d99f6

Please sign in to comment.