Skip to content

Commit

Permalink
manager should not panic and ignore wrong Clusterscoped type setting …
Browse files Browse the repository at this point in the history
…in HNCConfiguration
  • Loading branch information
Abirdcfly committed Aug 18, 2021
1 parent 8af53fa commit bbf990b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/v1alpha2/hnc_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const (
// Condition reasons for BadConfiguration
ReasonMultipleConfigsForType = "MultipleConfigurationsForType"
ReasonResourceNotFound = "ResourceNotFound"
ReasonResourceNotNamespaced = "ResourceNotNamespaced"

// Condition reason for OutOfSync, e.g. errors when creating a reconciler.
ReasonUnknown = "Unknown"
Expand Down
9 changes: 8 additions & 1 deletion internal/reconcilers/hnc_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ func (r *ConfigReconciler) reconcileConfigTypes(inst *api.HNCConfiguration, allR
// If the type is not found, log error and write conditions but don't
// early exit since the other types can still be reconciled.
r.Log.Error(err, "while trying to reconcile the configuration", "type", gr, "mode", rsc.Mode)
r.writeCondition(inst, api.ConditionBadTypeConfiguration, api.ReasonResourceNotFound, err.Error())
if strings.Contains(err.Error(), "not found") {
r.writeCondition(inst, api.ConditionBadTypeConfiguration, api.ReasonResourceNotFound, err.Error())
} else {
r.writeCondition(inst, api.ConditionBadTypeConfiguration, api.ReasonResourceNotNamespaced, err.Error())
}
continue
}
r.activeGVKMode[gr] = gvkMode{gvk, rsc.Mode}
Expand Down Expand Up @@ -599,6 +603,9 @@ func GVKFor(gr schema.GroupResource, allRes []*restmapper.APIGroupResources) (sc
for _, version := range group.Versions {
for _, resource := range groupedResources.VersionedResources[version.Version] {
if resource.Name == gr.Resource {
if !resource.Namespaced {
return schema.GroupVersionKind{}, fmt.Errorf("Resource %q is not namespaced", gr)
}
// Please note that we cannot use resource.group or resource.version
// here because they are preferred group/version and they are default
// to empty to imply this current containing group/version. Therefore,
Expand Down
8 changes: 8 additions & 0 deletions internal/reconcilers/hnc_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ var _ = Describe("HNCConfiguration", func() {
Expect(objectInheritedFrom(ctx, "crontabs", barName, "foo-crontab")).Should(Equal(fooName))
})

It("manager should not panic and ignore wrong Clusterscoped type setting in HNCConfiguration", func() {
// Add a config for a type that hasn't been defined yet.
addToHNCConfig(ctx, api.RBACGroup, "clusterroles", api.Propagate)

Eventually(getHNCConfigCondition(ctx, api.ConditionBadTypeConfiguration, api.ReasonResourceNotNamespaced)).
Should(ContainSubstring("Resource \"clusterroles.rbac.authorization.k8s.io\" is not namespaced"))
})

It("should set NumPropagatedObjects back to 0 after deleting the source object in propagate mode", func() {
addToHNCConfig(ctx, "", "limitranges", api.Propagate)
setParent(ctx, barName, fooName)
Expand Down

0 comments on commit bbf990b

Please sign in to comment.