-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: build resource-group image in repo (#959)
This changes the resource-group-controller image to be built alongside the other first party images rather than built separately and imported as a third party image. The ResourceGroup controller is a core component of Config Sync, and this is intended to simplify the maintenance of this component.
- Loading branch information
Showing
33 changed files
with
3,424 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// 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 main | ||
|
||
import ( | ||
"os" | ||
|
||
"kpt.dev/resourcegroup/controllers/runner" | ||
) | ||
|
||
func main() { | ||
os.Exit(runner.Run()) | ||
} |
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
85 changes: 85 additions & 0 deletions
85
vendor/kpt.dev/resourcegroup/controllers/handler/crd_event_handler.go
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,85 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// 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 handler | ||
|
||
import ( | ||
"github.com/go-logr/logr" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/util/workqueue" | ||
"kpt.dev/resourcegroup/apis/kpt.dev/v1alpha1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
) | ||
|
||
// CRDEventHandler pushes an event to ResourceGroup event channel | ||
// when the CRD or its CRs are contained in some ResourceGroup CRs. | ||
type CRDEventHandler struct { | ||
Mapping resourceMap | ||
Channel chan event.GenericEvent | ||
Log logr.Logger | ||
} | ||
|
||
var _ handler.EventHandler = &CRDEventHandler{} | ||
|
||
// Create implements EventHandler | ||
func (h *CRDEventHandler) Create(e event.CreateEvent, _ workqueue.RateLimitingInterface) { | ||
h.Log.V(5).Info("received a create event") | ||
h.enqueueEvent(e.Object) | ||
} | ||
|
||
// Update implements EventHandler | ||
func (h *CRDEventHandler) Update(e event.UpdateEvent, _ workqueue.RateLimitingInterface) { | ||
h.Log.V(5).Info("received an update event") | ||
h.enqueueEvent(e.ObjectNew) | ||
} | ||
|
||
// Delete implements EventHandler | ||
func (h *CRDEventHandler) Delete(e event.DeleteEvent, _ workqueue.RateLimitingInterface) { | ||
h.Log.V(5).Info("received a delete event") | ||
h.enqueueEvent(e.Object) | ||
} | ||
|
||
// Generic implements EventHandler | ||
func (h *CRDEventHandler) Generic(e event.GenericEvent, _ workqueue.RateLimitingInterface) { | ||
h.Log.V(5).Info("received a generic event") | ||
h.enqueueEvent(e.Object) | ||
} | ||
|
||
func (h *CRDEventHandler) enqueueEvent(obj client.Object) { | ||
crd, ok := obj.(*apiextensionsv1.CustomResourceDefinition) | ||
if !ok { | ||
h.Log.Info("failed to derive a CRD from the event object", "name", obj.GetName()) | ||
return | ||
} | ||
gk := schema.GroupKind{ | ||
Group: crd.Spec.Group, | ||
Kind: crd.Spec.Names.Kind, | ||
} | ||
for _, gknn := range h.Mapping.GetResources(gk) { | ||
// clear the cached status for gknn since the CR status could change | ||
// due to the change of CRD. | ||
h.Log.V(5).Info("reset the cached resource status", "resource", gknn) | ||
h.Mapping.SetStatus(gknn, nil) | ||
for _, r := range h.Mapping.Get(gknn) { | ||
var resgroup = &v1alpha1.ResourceGroup{} | ||
resgroup.SetNamespace(r.Namespace) | ||
resgroup.SetName(r.Name) | ||
h.Log.V(5).Info("send a generic event for", "resourcegroup", resgroup.GetObjectMeta()) | ||
h.Channel <- event.GenericEvent{Object: resgroup} | ||
} | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
vendor/kpt.dev/resourcegroup/controllers/handler/event_handler.go
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,102 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// 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 handler | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-logr/logr" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/tools/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
|
||
"kpt.dev/resourcegroup/apis/kpt.dev/v1alpha1" | ||
"kpt.dev/resourcegroup/controllers/resourcemap" | ||
) | ||
|
||
// resourceMap provides the interface for access the cached resources. | ||
type resourceMap interface { | ||
// Get maps from GKNN -> []RG. It gets the identifiers of ResourceGroup CRs | ||
// that a GKNN is in. | ||
Get(gknn v1alpha1.ObjMetadata) []types.NamespacedName | ||
// GetResources map from GK -> []GKNN. It gets the list of GKNN for | ||
// a given group kind. | ||
GetResources(gk schema.GroupKind) []v1alpha1.ObjMetadata | ||
// SetStatus sets the cached status for the given resource. | ||
SetStatus(res v1alpha1.ObjMetadata, resStatus *resourcemap.CachedStatus) | ||
} | ||
|
||
// EnqueueEventToChannel pushes an event to ResourceGroup event channel | ||
// instead of enqueue a Reqeust for ResourceGroup. | ||
type EnqueueEventToChannel struct { | ||
Mapping resourceMap | ||
Channel chan event.GenericEvent | ||
Log logr.Logger | ||
GVK schema.GroupVersionKind | ||
} | ||
|
||
var _ cache.ResourceEventHandler = &EnqueueEventToChannel{} | ||
|
||
// Create implements EventHandler | ||
func (e *EnqueueEventToChannel) OnAdd(obj interface{}) { | ||
e.Log.V(5).Info("received an add event") | ||
e.enqueueEvent(obj) | ||
} | ||
|
||
// Update implements EventHandler | ||
func (e *EnqueueEventToChannel) OnUpdate(_, newObj interface{}) { | ||
e.Log.V(5).Info("received an update event") | ||
e.enqueueEvent(newObj) | ||
} | ||
|
||
// Delete implements EventHandler | ||
func (e *EnqueueEventToChannel) OnDelete(obj interface{}) { | ||
e.Log.V(5).Info("received a delete event") | ||
e.enqueueEvent(obj) | ||
} | ||
|
||
func (e *EnqueueEventToChannel) enqueueEvent(obj interface{}) { | ||
gknn, err := e.toGKNN(obj) | ||
if err != nil { | ||
e.Log.Error(err, "failed to get GKNN from the received event", "object", obj) | ||
return | ||
} | ||
for _, r := range e.Mapping.Get(gknn) { | ||
var resgroup = &v1alpha1.ResourceGroup{} | ||
resgroup.SetNamespace(r.Namespace) | ||
resgroup.SetName(r.Name) | ||
e.Log.V(5).Info("send a generic event for", "resourcegroup", resgroup.GetObjectMeta()) | ||
e.Channel <- event.GenericEvent{Object: resgroup} | ||
} | ||
} | ||
|
||
func (e EnqueueEventToChannel) toGKNN(obj interface{}) (v1alpha1.ObjMetadata, error) { | ||
metadata, err := meta.Accessor(obj) | ||
if err != nil { | ||
e.Log.Error(err, "missing object meta") | ||
return v1alpha1.ObjMetadata{}, fmt.Errorf("missing object meta: %v", err) | ||
} | ||
gknn := v1alpha1.ObjMetadata{ | ||
Namespace: metadata.GetNamespace(), | ||
Name: metadata.GetName(), | ||
GroupKind: v1alpha1.GroupKind{ | ||
Group: e.GVK.Group, | ||
Kind: e.GVK.Kind, | ||
}, | ||
} | ||
return gknn, nil | ||
} |
Oops, something went wrong.