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

Commit 755b769

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

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-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+
ReasonResourceNotNamescoped = "ResourceNotNamescoped"
6364

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

internal/reconcilers/hnc_config.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,19 @@ func (r *ConfigReconciler) reconcileConfigTypes(inst *api.HNCConfiguration, allR
186186
}
187187

188188
// Look if the resource exists in the API server.
189-
gvk, err := GVKFor(gr, allRes)
189+
gvk, namescoped, err := GVKNamescopedFor(gr, allRes)
190190
if err != nil {
191191
// If the type is not found, log error and write conditions but don't
192192
// early exit since the other types can still be reconciled.
193193
r.Log.Error(err, "while trying to reconcile the configuration", "type", gr, "mode", rsc.Mode)
194194
r.writeCondition(inst, api.ConditionBadTypeConfiguration, api.ReasonResourceNotFound, err.Error())
195195
continue
196196
}
197+
if !namescoped {
198+
r.Log.Error(err, "while trying to reconcile the configuration", "type", gr, "mode", rsc.Mode)
199+
r.writeCondition(inst, api.ConditionBadTypeConfiguration, api.ReasonResourceNotNamescoped, fmt.Sprintf("type %s is not Namescoped", gr))
200+
continue
201+
}
197202
r.activeGVKMode[gr] = gvkMode{gvk, rsc.Mode}
198203
r.activeGR[gvk] = gr
199204
}
@@ -586,6 +591,13 @@ func GetAllResources(config *rest.Config) ([]*restmapper.APIGroupResources, erro
586591
// GVKFor searches the GR in apiserver and returns the mapping GVK. If the GR
587592
// doesn't exist, return an empty GVK and the error.
588593
func GVKFor(gr schema.GroupResource, allRes []*restmapper.APIGroupResources) (schema.GroupVersionKind, error) {
594+
gvk, _, err := GVKNamescopedFor(gr, allRes)
595+
return gvk, err
596+
}
597+
598+
// GVKNamescopedFor searches the GR in apiserver and returns the mapping GVK and whether it is namespaced. If the GR
599+
// doesn't exist, return an empty GVK and the error.
600+
func GVKNamescopedFor(gr schema.GroupResource, allRes []*restmapper.APIGroupResources) (schema.GroupVersionKind, bool, error) {
589601
// Look for a matching resource from all resources.
590602
for _, groupedResources := range allRes {
591603
group := groupedResources.Group
@@ -608,10 +620,10 @@ func GVKFor(gr schema.GroupResource, allRes []*restmapper.APIGroupResources) (sc
608620
Version: version.Version,
609621
Kind: resource.Kind,
610622
}
611-
return gvk, nil
623+
return gvk, resource.Namespaced, nil
612624
}
613625
}
614626
}
615627
}
616-
return schema.GroupVersionKind{}, fmt.Errorf("Resource %q not found", gr)
628+
return schema.GroupVersionKind{}, false, fmt.Errorf("Resource %q not found", gr)
617629
}

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.ReasonResourceNotNamescoped)).
295+
Should(ContainSubstring("type clusterroles.rbac.authorization.k8s.io is not Namescoped"))
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)