-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generic code from the controller runtime is used to retrieve unstructured objects. This is needed for deployment via Deployment, because the direct parent of the pod is then a ReplicaSet which itself will get deleted by the Deployment when rolling out changes. There are intentionally no unit tests for the feature because that would bring in even more additional dependencies.
- Loading branch information
Showing
36 changed files
with
3,589 additions
and
49 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,92 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
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 | ||
http://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 owner contains code for walking up the ownership chain, | ||
// starting with an arbitrary object. RBAC rules must allow GET access | ||
// to each object on the chain, at least including the starting | ||
// object, more when walking up more than one level. | ||
package owner | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/rest" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// Lookup walks up the ownership chain zero or more levels and returns an OwnerReference for the | ||
// object. The object identified by name, namespace and type is the starting point and is | ||
// returned when levels is zero. Only APIVersion, Kind, Name, and UID will be set. | ||
func Lookup(config *rest.Config, namespace, name string, gkv schema.GroupVersionKind, levels int) (*metav1.OwnerReference, error) { | ||
c, err := client.New(config, client.Options{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("build client: %v", err) | ||
} | ||
|
||
return lookupRecursive(c, namespace, name, gkv, levels) | ||
} | ||
|
||
func lookupRecursive(c client.Client, namespace, name string, gkv schema.GroupVersionKind, levels int) (*metav1.OwnerReference, error) { | ||
u := &unstructured.Unstructured{} | ||
u.SetGroupVersionKind(gkv) | ||
|
||
if err := c.Get(context.Background(), client.ObjectKey{ | ||
Namespace: namespace, | ||
Name: name, | ||
}, u); err != nil { | ||
return nil, fmt.Errorf("get object: %v", err) | ||
} | ||
|
||
if levels == 0 { | ||
return &metav1.OwnerReference{ | ||
APIVersion: metav1.GroupVersion{Group: gkv.Group, Version: gkv.Version}.String(), | ||
Kind: gkv.Kind, | ||
Name: name, | ||
UID: u.GetUID(), | ||
}, nil | ||
} | ||
owners := u.GetOwnerReferences() | ||
for _, owner := range owners { | ||
if owner.Controller != nil && *owner.Controller { | ||
gv, err := schema.ParseGroupVersion(owner.APIVersion) | ||
if err != nil { | ||
return nil, fmt.Errorf("parse OwnerReference.APIVersion: %v", err) | ||
} | ||
// With this special case here we avoid one lookup and thus the need for | ||
// more RBAC permissions. | ||
if levels == 1 { | ||
return &metav1.OwnerReference{ | ||
APIVersion: owner.APIVersion, | ||
Kind: owner.Kind, | ||
Name: owner.Name, | ||
UID: owner.UID, | ||
}, nil | ||
} | ||
|
||
return lookupRecursive(c, namespace, owner.Name, schema.GroupVersionKind{ | ||
Group: gv.Group, | ||
Version: gv.Version, | ||
Kind: owner.Kind, | ||
}, levels-1) | ||
} | ||
} | ||
return nil, fmt.Errorf("%s %q in namespace %q has no controlling owner, cannot unwind the ownership further", | ||
gkv.String(), name, namespace) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.