Skip to content

Commit

Permalink
Merge pull request #59 from just1900/master
Browse files Browse the repository at this point in the history
Allow HNC to propagate changes to CRD.
  • Loading branch information
k8s-ci-robot authored Jul 16, 2021
2 parents fc62267 + 5b9e3cb commit 291134a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ manifests
*.swp
*.swo
*~
.vscode
7 changes: 7 additions & 0 deletions internal/reconcilers/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,15 @@ func (r *ObjectReconciler) writeObject(ctx context.Context, log logr.Logger, ins
// The object exists if CreationTimestamp is set. This flag enables us to have only 1 API call.
exist := inst.GetCreationTimestamp() != v1.Time{}
ns := inst.GetNamespace()
// Get current ResourceVersion, required for updates for newer API objects (including custom resources).
rv := inst.GetResourceVersion()

// Overwrite the propagated copy with the source, then restore all essential properties of the copy.
inst = object.Canonical(srcInst)
inst.SetNamespace(ns)
// We should set the resourceVersion when updating the object, which is required by k8s.
// for more details see https://github.com/kubernetes-sigs/hierarchical-namespaces/issues/53.
inst.SetResourceVersion(rv)
metadata.SetLabel(inst, api.LabelInheritedFrom, srcInst.GetNamespace())
log.V(1).Info("Writing", "dst", inst.GetNamespace(), "origin", srcInst.GetNamespace())

Expand Down
14 changes: 14 additions & 0 deletions pkg/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,20 @@ func CleanupTestNamespaces() {
cleanupNamespaces(nses...)
}

// CleanupCRDIfExists checks whether the eetests.e2e.hnc.x-k8s.io CRD is present, if so, delete it.
func CleanupTestCRDIfExists() {
crd := "eetests.e2e.hnc.x-k8s.io"
if err := TryRunQuietly("kubectl get crd", crd); err != nil {
// crd does not exist, return directly.
return
}
// undo this before deleting the crd, otherwise webhook will deny the request.
MustRun("kubectl hns config delete-type --group e2e.hnc.x-k8s.io --resource eetests")
MustRun("kubectl delete crd", crd)
// make sure the crd has been deleted.
RunShouldNotContain(crd, 10, "kubectl get crd")
}

// cleanupNamespaces does everything it can to delete the passed-in namespaces. It also uses very
// high timeouts (30s) since this function is often called after HNC has just been reinstalled, and
// it can take a while of HNC to start allowing changes to namespaces again.
Expand Down
80 changes: 78 additions & 2 deletions test/e2e/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var _ = Describe("Issues", func() {

AfterEach(func() {
CleanupTestNamespaces()
CleanupTestCRDIfExists()
})

It("Should not delete full namespace when a faulty anchor is deleted - issue #1149", func() {
Expand Down Expand Up @@ -264,6 +265,81 @@ var _ = Describe("Issues", func() {
// config will be in the failure log and we can see what's happened.
MustNotRun("kubectl get -oyaml hierarchyconfiguration hierarchy -n", nsParent)
})

It("Should propagate the change of source CRD - issue #53", func() {
// Create a parent namespace and a subnamespace for it.
CreateNamespace(nsParent)
MustRun("kubectl get ns", nsParent)
CreateSubnamespace(nsChild, nsParent)
crd := `# a simple CRD used for e2e testing only, should be deleted after finishing(or failing) this testcase.
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: eetests.e2e.hnc.x-k8s.io
spec:
group: e2e.hnc.x-k8s.io
names:
kind: EETest
listKind: EETestList
plural: eetests
singular: eetest
scope: Namespaced
versions:
- name: v1
schema:
openAPIV3Schema:
description: EETest is the Schema for the eetests API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: EETestSpec defines the desired state of EETest
properties:
foo:
description: Foo is an example field of EETest. Edit eetest_types.go
to remove/update
type: string
type: object
type: object
served: true
storage: true`
// create CRD and set the propagation strategy for it.
MustApplyYAML(crd)
MustRun("kubectl hns config set-resource eetests --group e2e.hnc.x-k8s.io --mode Propagate --force")

eetest := `# this is an instance of CRD eetests.e2e.hnc.x-k8s.io/v1
apiVersion: e2e.hnc.x-k8s.io/v1
kind: EETest
metadata:
name: eetest-sample
namespace: parent
spec:
foo: foo`
MustApplyYAML(eetest)
// check this crd is propagated to subns successfully.
MustRunWithTimeout(60, "kubectl get eetest eetest-sample -n", nsChild, "-oyaml")
// perform an update to eetest in parent ns.
eetestUpdated := `# set field foo's value to bar to perform an update.
apiVersion: e2e.hnc.x-k8s.io/v1
kind: EETest
metadata:
name: eetest-sample
namespace: parent
spec:
foo: bar`
MustApplyYAML(eetestUpdated)
FieldShouldContainWithTimeout("eetests.e2e.hnc.x-k8s.io", nsChild, "eetest-sample", ".spec.foo", "bar", 30)
})
})

var _ = Describe("Issues with bad anchors", func() {
Expand Down Expand Up @@ -338,8 +414,8 @@ var _ = Describe("Issues with bad anchors", func() {

var _ = Describe("Issues that require repairing HNC", func() {
const (
nsParent = "parent"
nsChild = "child"
nsParent = "parent"
nsChild = "child"
nsNonExcluded = "regular"
// Use `hnc-system` for this test because HNC excludes `hnc-system`
//namespace by default.
Expand Down

0 comments on commit 291134a

Please sign in to comment.