-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Squashed commit of the following: (#1318)
commit 3422bf620ace313c5b806986579d498d48404f04 Author: Jingfang Liu <jingfangliu@google.com> Date: Mon Dec 28 13:55:32 2020 -0800 change the order of updating owning-inventory annotation and creating ResourceGorup CR commit be90e247a8729aa7321bd3b23541894805f26cc4 Author: Jingfang Liu <jingfangliu@google.com> Date: Tue Dec 22 16:16:46 2020 -0800 fix lint error commit e9c9c3869268424184eb07647c1f47790c468e28 Author: Jingfang Liu <jingfangliu@google.com> Date: Tue Dec 22 14:44:52 2020 -0800 update the last-applied-configuration properly commit 901528a3ff3b89f4fc163d09d75dc02ac04cf4e0 Author: Jingfang Liu <jingfangliu@google.com> Date: Tue Dec 22 14:16:41 2020 -0800 update owning-inventory annotation during migration
- Loading branch information
1 parent
76810ac
commit 8201c64
Showing
7 changed files
with
263 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2020 Google LLC. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/kubectl/pkg/scheme" | ||
"k8s.io/kubectl/pkg/util" | ||
"sigs.k8s.io/cli-utils/pkg/object" | ||
) | ||
|
||
// client is the client to update object in the API server. | ||
type client struct { | ||
client dynamic.Interface | ||
restMapper meta.RESTMapper | ||
} | ||
|
||
func NewClient(d dynamic.Interface, mapper meta.RESTMapper) *client { | ||
return &client{ | ||
client: d, | ||
restMapper: mapper, | ||
} | ||
} | ||
|
||
// Update updates an object using dynamic client | ||
func (uc *client) Update(ctx context.Context, meta object.ObjMetadata, obj *unstructured.Unstructured, options *metav1.UpdateOptions) error { | ||
r, err := uc.resourceInterface(meta) | ||
if err != nil { | ||
return err | ||
} | ||
if options == nil { | ||
options = &metav1.UpdateOptions{} | ||
} | ||
_, err = r.Update(ctx, obj, *options) | ||
return err | ||
} | ||
|
||
// Get fetches the requested object into the input obj using dynamic client | ||
func (uc *client) Get(ctx context.Context, meta object.ObjMetadata) (*unstructured.Unstructured, error) { | ||
r, err := uc.resourceInterface(meta) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return r.Get(ctx, meta.Name, metav1.GetOptions{}) | ||
} | ||
|
||
func (uc *client) resourceInterface(meta object.ObjMetadata) (dynamic.ResourceInterface, error) { | ||
mapping, err := uc.restMapper.RESTMapping(meta.GroupKind) | ||
if err != nil { | ||
return nil, err | ||
} | ||
namespacedClient := uc.client.Resource(mapping.Resource).Namespace(meta.Namespace) | ||
return namespacedClient, nil | ||
} | ||
|
||
// UpdateAnnotation updates the object owning inventory annotation | ||
// to the new ID when the owning inventory annotation is either empty or the old ID. | ||
// It returns if the annotation is updated. | ||
func UpdateAnnotation(obj *unstructured.Unstructured, oldID, newID string) (bool, error) { | ||
key := "config.k8s.io/owning-inventory" | ||
annotations := obj.GetAnnotations() | ||
if annotations == nil { | ||
annotations = make(map[string]string) | ||
} | ||
val, found := annotations[key] | ||
if !found || val == oldID { | ||
annotations[key] = newID | ||
// Since the annotation is updated, we also need to update the | ||
// last applied configuration annotation. | ||
u := getOriginalObj(obj) | ||
if u != nil { | ||
u.SetAnnotations(annotations) | ||
err := util.CreateOrUpdateAnnotation(false, u, scheme.DefaultJSONEncoder()) | ||
obj.SetAnnotations(u.GetAnnotations()) | ||
return true, err | ||
} | ||
obj.SetAnnotations(annotations) | ||
return true, nil | ||
} | ||
return false, nil | ||
} | ||
|
||
func getOriginalObj(obj *unstructured.Unstructured) *unstructured.Unstructured { | ||
annotations := obj.GetAnnotations() | ||
lastApplied, found := annotations[v1.LastAppliedConfigAnnotation] | ||
if !found { | ||
return nil | ||
} | ||
u := &unstructured.Unstructured{} | ||
err := json.Unmarshal([]byte(lastApplied), u) | ||
if err != nil { | ||
return nil | ||
} | ||
return u | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2020 Google LLC. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package client | ||
|
||
import ( | ||
"testing" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
func TestUpdateAnnotation(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
annotations map[string]string | ||
oldID string | ||
newID string | ||
shouldUpdate bool | ||
}{ | ||
{ | ||
name: "empty owning-inventory is changed to newID", | ||
annotations: nil, | ||
oldID: "old", | ||
newID: "new", | ||
shouldUpdate: true, | ||
}, | ||
{ | ||
name: "oldID is changed to newID", | ||
annotations: map[string]string{ | ||
"config.k8s.io/owning-inventory": "old", | ||
}, | ||
oldID: "old", | ||
newID: "new", | ||
shouldUpdate: true, | ||
}, | ||
{ | ||
name: "non empty unmatched id won't be changed to newID", | ||
annotations: map[string]string{ | ||
"config.k8s.io/owning-inventory": "random", | ||
}, | ||
oldID: "old", | ||
newID: "new", | ||
shouldUpdate: false, | ||
}, | ||
} | ||
deployment := &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"apiVersion": "apps/v1", | ||
"kind": "Deployment", | ||
"metadata": map[string]interface{}{ | ||
"name": "deployment", | ||
"namespace": "test", | ||
}, | ||
}, | ||
} | ||
for _, tc := range testcases { | ||
deployment.SetAnnotations(tc.annotations) | ||
updated, err := UpdateAnnotation(deployment, tc.oldID, tc.newID) | ||
if err != nil { | ||
t.Errorf("unexpected error %v", err) | ||
} | ||
if tc.shouldUpdate { | ||
if !updated { | ||
t.Errorf("owning-inventory should be updated") | ||
} | ||
if deployment.GetAnnotations()["config.k8s.io/owning-inventory"] != tc.newID { | ||
t.Errorf("the owning-inventory annotation is not correctly updated") | ||
} | ||
} else if updated { | ||
t.Errorf("owning-inventory shouldn't be changed") | ||
} | ||
} | ||
} |