Skip to content

Commit 7c1c7d9

Browse files
committed
simplify scaffolding
1 parent 409a392 commit 7c1c7d9

File tree

2 files changed

+14
-30
lines changed

2 files changed

+14
-30
lines changed

scaffold/templates/api/v1alpha1/types.go.tpl

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@ limitations under the License.
1717
package {{ .groupVersion }}
1818

1919
import (
20-
"context"
21-
2220
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2321
runtime "k8s.io/apimachinery/pkg/runtime"
24-
"sigs.k8s.io/controller-runtime/pkg/client"
2522

2623
"github.com/sap/component-operator-runtime/pkg/component"
2724
componentoperatorruntimetypes "github.com/sap/component-operator-runtime/pkg/types"
2825
)
2926

3027
// {{ .kind }}Spec defines the desired state of {{ .kind }}
3128
type {{ .kind }}Spec struct {
32-
// You can remove component.Spec, but then you have to provide your own (meaningful)
33-
// implementations of GetDeploymentNamespace() and GetDeploymentName() below.
34-
component.Spec `json:",inline"`
29+
// Uncomment the following if you want to make deployment namespace and name configurable
30+
// here in the spec (independently of the component's metadata.namespace and metadata.name);
31+
// if you do, also review the implementations of GetDeploymentNamespace() and GetDeploymentName() below.
32+
// component.Spec `json:",inline"`
33+
3534
// Add your own fields here, describing the deployment of the managed component.
3635
}
3736
3837
// {{ .kind }}Status defines the observed state of {{ .kind }}
3938
type {{ .kind }}Status struct {
4039
component.Status `json:",inline"`
40+
4141
// You may add your own fields here; this is rarely needed.
4242
}
4343
@@ -76,16 +76,18 @@ func (s *{{ .kind }}Spec) ToUnstructured() map[string]any {
7676
}
7777
7878
func (c *{{ .kind }}) GetDeploymentNamespace() string {
79-
if c.Spec.Namespace != "" {
80-
return c.Spec.Namespace
81-
}
79+
// Uncomment the following if you allow specification of deployment namespace via the component spec.
80+
// if c.Spec.Namespace != "" {
81+
// return c.Spec.Namespace
82+
// }
8283
return c.Namespace
8384
}
8485
8586
func (c *{{ .kind }}) GetDeploymentName() string {
86-
if c.Spec.Name != "" {
87-
return c.Spec.Name
88-
}
87+
// Uncomment the following if you allow specification of deployment name via the component spec.
88+
// if c.Spec.Name != "" {
89+
// return c.Spec.Name
90+
// }
8991
return c.Name
9092
}
9193
@@ -97,22 +99,6 @@ func (c *{{ .kind }}) GetStatus() *component.Status {
9799
return &c.Status.Status
98100
}
99101
100-
// The following post read hook ensures that Spec.Namespace and Spec.Name are properly defaulted (as metadata.namespace/metadata.name).
101-
// The hook will be called by the reconciler after retrieving the component object from the Kubernetes API.
102-
// Of course, the same could be (better) achieved by a mutating admission webhook, but we strive to build component operators
103-
// without admission webhooks.
104-
// You can remove this hook (and its registration in pkg/operator/operator.go) if you do not use .Spec.Namespace or .Spec.Name anywhere,
105-
// or if you have chosen not to include component.Spec in {{.kind}}Spec above.
106-
func PostReadHook(ctx context.Context, client client.Client, c *{{ .kind }}) error {
107-
if c.Spec.Namespace == "" {
108-
c.Spec.Namespace = c.Namespace
109-
}
110-
if c.Spec.Name == "" {
111-
c.Spec.Name = c.Name
112-
}
113-
return nil
114-
}
115-
116102
func init() {
117103
SchemeBuilder.Register(&{{ .kind }}{}, &{{ .kind }}List{})
118104
}

scaffold/templates/pkg/operator/operator.go.tpl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ func (o *Operator) Setup(mgr ctrl.Manager, discoveryClient discovery.DiscoveryIn
117117
mgr.GetEventRecorderFor(o.options.Name),
118118
mgr.GetScheme(),
119119
resourceGenerator,
120-
).WithPostReadHook(
121-
operatorv1alpha1.PostReadHook,
122120
).SetupWithManager(mgr); err != nil {
123121
return errors.Wrapf(err, "unable to create controller")
124122
}

0 commit comments

Comments
 (0)