Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 5e7e9b2

Browse files
committed
manager should not panic and ignore wrong Clusterscoped type setting in HNCConfiguration
1 parent 8af53fa commit 5e7e9b2

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

api/v1alpha2/hnc_config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const (
6060
// Condition reasons for BadConfiguration
6161
ReasonMultipleConfigsForType = "MultipleConfigurationsForType"
6262
ReasonResourceNotFound = "ResourceNotFound"
63+
ReasonResourceNotNamespaced = "ResourceNotNamespaced"
6364

6465
// Condition reason for OutOfSync, e.g. errors when creating a reconciler.
6566
ReasonUnknown = "Unknown"

internal/reconcilers/hnc_config.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ type gr2gvkMode map[schema.GroupResource]gvkMode
7878
// gvk2gr keeps track of a group of unique GVKs with the mapping GRs.
7979
type gvk2gr map[schema.GroupVersionKind]schema.GroupResource
8080

81+
type GVKErr struct {
82+
Reason string
83+
Msg string
84+
}
85+
86+
func (e *GVKErr) Error() string {
87+
return e.Msg
88+
}
89+
8190
// checkPeriod is the period that the config reconciler checks if it needs to reconcile the
8291
// `config` singleton.
8392
const checkPeriod = 3 * time.Second
@@ -188,10 +197,12 @@ func (r *ConfigReconciler) reconcileConfigTypes(inst *api.HNCConfiguration, allR
188197
// Look if the resource exists in the API server.
189198
gvk, err := GVKFor(gr, allRes)
190199
if err != nil {
191-
// If the type is not found, log error and write conditions but don't
200+
// If the type is not found or namespaced, log error and write conditions but don't
192201
// early exit since the other types can still be reconciled.
193202
r.Log.Error(err, "while trying to reconcile the configuration", "type", gr, "mode", rsc.Mode)
194-
r.writeCondition(inst, api.ConditionBadTypeConfiguration, api.ReasonResourceNotFound, err.Error())
203+
if gvkerr, ok := err.(*GVKErr); ok {
204+
r.writeCondition(inst, api.ConditionBadTypeConfiguration, gvkerr.Reason, gvkerr.Msg)
205+
}
195206
continue
196207
}
197208
r.activeGVKMode[gr] = gvkMode{gvk, rsc.Mode}
@@ -599,6 +610,9 @@ func GVKFor(gr schema.GroupResource, allRes []*restmapper.APIGroupResources) (sc
599610
for _, version := range group.Versions {
600611
for _, resource := range groupedResources.VersionedResources[version.Version] {
601612
if resource.Name == gr.Resource {
613+
if !resource.Namespaced {
614+
return schema.GroupVersionKind{}, &GVKErr{api.ReasonResourceNotNamespaced, fmt.Sprintf("Resource %q is not namespaced", gr)}
615+
}
602616
// Please note that we cannot use resource.group or resource.version
603617
// here because they are preferred group/version and they are default
604618
// to empty to imply this current containing group/version. Therefore,
@@ -613,5 +627,5 @@ func GVKFor(gr schema.GroupResource, allRes []*restmapper.APIGroupResources) (sc
613627
}
614628
}
615629
}
616-
return schema.GroupVersionKind{}, fmt.Errorf("Resource %q not found", gr)
630+
return schema.GroupVersionKind{}, &GVKErr{api.ReasonResourceNotFound, fmt.Sprintf("Resource %q not found", gr)}
617631
}

internal/reconcilers/hnc_config_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@ var _ = Describe("HNCConfiguration", func() {
287287
Expect(objectInheritedFrom(ctx, "crontabs", barName, "foo-crontab")).Should(Equal(fooName))
288288
})
289289

290+
It("manager should not panic and ignore wrong Clusterscoped type setting in HNCConfiguration", func() {
291+
// Add a config for a type that hasn't been defined yet.
292+
addToHNCConfig(ctx, api.RBACGroup, "clusterroles", api.Propagate)
293+
294+
Eventually(getHNCConfigCondition(ctx, api.ConditionBadTypeConfiguration, api.ReasonResourceNotNamespaced)).
295+
Should(ContainSubstring("Resource \"clusterroles.rbac.authorization.k8s.io\" is not namespaced"))
296+
})
297+
290298
It("should set NumPropagatedObjects back to 0 after deleting the source object in propagate mode", func() {
291299
addToHNCConfig(ctx, "", "limitranges", api.Propagate)
292300
setParent(ctx, barName, fooName)

0 commit comments

Comments
 (0)