Skip to content

Commit

Permalink
feat: update reconciler builder, and test cluster, role, resource rec…
Browse files Browse the repository at this point in the history
…oncile (#64)

* feat: add more test, and update reconciler builder

* feat: refactor builder and config to accept name, labels, and annotations as separate parameters
  • Loading branch information
whg517 authored Jul 5, 2024
1 parent b5a96a3 commit c221b91
Show file tree
Hide file tree
Showing 23 changed files with 641 additions and 365 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,6 @@ Temporary Items
.apdisk

bin/
testbin/
testbin/

**/coverage/
38 changes: 24 additions & 14 deletions pkg/builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,18 @@ type BaseConfigBuilder struct {
data map[string]string
}

type ConfigBuilderOptions struct {
Name string
Labels map[string]string
Annotations map[string]string
}

func NewBaseConfigBuilder(
client *client.Client,
options *ConfigBuilderOptions,
name string,
labels map[string]string,
annotations map[string]string,
) *BaseConfigBuilder {
return &BaseConfigBuilder{
BaseResourceBuilder: BaseResourceBuilder{
Client: client,
name: options.Name,
labels: options.Labels,
annotations: options.Annotations,
name: name,
labels: labels,
annotations: annotations,
},
data: make(map[string]string),
}
Expand Down Expand Up @@ -91,10 +87,17 @@ type ConfigMapBuilder struct {

func NewConfigMapBuilder(
client *client.Client,
options *ConfigBuilderOptions,
name string,
labels map[string]string,
annotations map[string]string,
) *ConfigMapBuilder {
return &ConfigMapBuilder{
BaseConfigBuilder: *NewBaseConfigBuilder(client, options),
BaseConfigBuilder: *NewBaseConfigBuilder(
client,
name,
labels,
annotations,
),
}
}

Expand All @@ -117,10 +120,17 @@ var _ ConfigBuilder = &SecretBuilder{}

func NewSecretBuilder(
client *client.Client,
options *ConfigBuilderOptions,
name string,
labels map[string]string,
annotations map[string]string,
) *SecretBuilder {
return &SecretBuilder{
BaseConfigBuilder: *NewBaseConfigBuilder(client, options),
BaseConfigBuilder: *NewBaseConfigBuilder(
client,
name,
labels,
annotations,
),
}
}

Expand Down
19 changes: 7 additions & 12 deletions pkg/builder/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

client "github.com/zncdatadev/operator-go/pkg/client"
"github.com/zncdatadev/operator-go/pkg/util"
appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -14,9 +13,9 @@ var (
DefaultReplicas = int32(1)
)

var _ DeploymentBuilder = &deployment{}
var _ DeploymentBuilder = &Deployment{}

type deployment struct {
type Deployment struct {
BaseWorkloadReplicasBuilder
}

Expand All @@ -26,15 +25,11 @@ func NewDeployment(
labels map[string]string,
annotations map[string]string,
affinity *corev1.Affinity,
image *util.Image,
ports []corev1.ContainerPort,
commandOverrides []string,
envOverrides map[string]string,
podOverrides *corev1.PodTemplateSpec,
terminationGracePeriodSeconds *int64,
replicas *int32,
) DeploymentBuilder {
return &deployment{
) *Deployment {
return &Deployment{
BaseWorkloadReplicasBuilder: *NewBaseWorkloadReplicasBuilder(
client,
name,
Expand All @@ -48,7 +43,7 @@ func NewDeployment(
}
}

func (b *deployment) GetObject() (*appv1.Deployment, error) {
func (b *Deployment) GetObject() (*appv1.Deployment, error) {
tpl, err := b.getPodTemplate()
if err != nil {
return nil, err
Expand All @@ -58,13 +53,13 @@ func (b *deployment) GetObject() (*appv1.Deployment, error) {
ObjectMeta: b.GetObjectMeta(),
Spec: appv1.DeploymentSpec{
Replicas: b.replicas,
Selector: b.GetSelector(),
Selector: b.GetLabelSelector(),
Template: *tpl,
},
}
return obj, nil
}

func (b *deployment) Build(_ context.Context) (ctrlclient.Object, error) {
func (b *Deployment) Build(_ context.Context) (ctrlclient.Object, error) {
return b.GetObject()
}
4 changes: 4 additions & 0 deletions pkg/builder/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type ResourceBuilder interface {
GetClient() *client.Client
SetName(name string)
GetName() string
AddLabels(labels map[string]string)
GetLabels() map[string]string
AddAnnotations(annotations map[string]string)
GetAnnotations() map[string]string
}

type WorkloadContainers interface {
Expand Down
2 changes: 1 addition & 1 deletion pkg/builder/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (b *jobBuilder) GetObject() (*batchv1.Job, error) {
obj := &batchv1.Job{
ObjectMeta: b.GetObjectMeta(),
Spec: batchv1.JobSpec{
Selector: b.GetSelector(),
Selector: b.GetLabelSelector(),
Template: *tpl,
},
}
Expand Down
57 changes: 31 additions & 26 deletions pkg/builder/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@ type GenericServiceAccountBuilder struct {
obj *corev1.ServiceAccount
}

type RBACBuilderOptions struct {
Name string
Labels map[string]string
Annotations map[string]string
}

func NewGenericServiceAccountBuilder(
client *resourceClient.Client,
options *RBACBuilderOptions,
name string,
labels map[string]string,
annotations map[string]string,
) *GenericServiceAccountBuilder {
return &GenericServiceAccountBuilder{
BaseResourceBuilder: BaseResourceBuilder{
Client: client,
name: options.Name,
labels: options.Labels,
Client: client,
name: name,
labels: labels,
annotations: annotations,
},
}
}
Expand Down Expand Up @@ -59,14 +56,16 @@ type GenericRoleBuilder struct {

func NewGenericRoleBuilder(
client *resourceClient.Client,
options *RBACBuilderOptions,
name string,
labels map[string]string,
annotations map[string]string,
) *GenericRoleBuilder {
return &GenericRoleBuilder{
BaseResourceBuilder: BaseResourceBuilder{
Client: client,
name: options.Name,
labels: options.Labels,
annotations: options.Annotations,
name: name,
labels: labels,
annotations: annotations,
},
}
}
Expand Down Expand Up @@ -94,14 +93,16 @@ type GenericRoleBindingBuilder struct {

func NewGenericRoleBindingBuilder(
client *resourceClient.Client,
options *RBACBuilderOptions,
name string,
labels map[string]string,
annotations map[string]string,
) *GenericRoleBindingBuilder {
return &GenericRoleBindingBuilder{
BaseResourceBuilder: BaseResourceBuilder{
Client: client,
name: options.Name,
labels: options.Labels,
annotations: options.Annotations,
name: name,
labels: labels,
annotations: annotations,
},
}
}
Expand Down Expand Up @@ -129,14 +130,16 @@ type GenericClusterRoleBuilder struct {

func NewGenericClusterRoleBuilder(
client *resourceClient.Client,
options *RBACBuilderOptions,
name string,
labels map[string]string,
annotations map[string]string,
) *GenericClusterRoleBuilder {
return &GenericClusterRoleBuilder{
BaseResourceBuilder: BaseResourceBuilder{
Client: client,
name: options.Name,
labels: options.Labels,
annotations: options.Annotations,
name: name,
labels: labels,
annotations: annotations,
},
}
}
Expand Down Expand Up @@ -166,14 +169,16 @@ type GenericClusterRoleBindingBuilder struct {

func NewGenericClusterRoleBindingBuilder(
client *resourceClient.Client,
options *RBACBuilderOptions,
name string,
labels map[string]string,
annotations map[string]string,
) *GenericClusterRoleBindingBuilder {
return &GenericClusterRoleBindingBuilder{
BaseResourceBuilder: BaseResourceBuilder{
Client: client,
name: options.Name,
labels: options.Labels,
annotations: options.Annotations,
name: name,
labels: labels,
annotations: annotations,
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/builder/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (b *BaseResourceBuilder) GetMatchingLabels() map[string]string {
return b.filterLabels(b.GetLabels())
}

func (b *BaseResourceBuilder) GetSelector() *metav1.LabelSelector {
func (b *BaseResourceBuilder) GetLabelSelector() *metav1.LabelSelector {
return &metav1.LabelSelector{
MatchLabels: b.GetMatchingLabels(),
}
Expand Down
20 changes: 8 additions & 12 deletions pkg/builder/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,22 @@ func (b *BaseServiceBuilder) Build(_ context.Context) (ctrlclient.Object, error)
return obj, nil
}

type ServiceBuilderOptions struct {
Name string
Labels map[string]string
Annotations map[string]string
Ports []corev1.ContainerPort
}

func NewServiceBuilder(
client *client.Client,
options *ServiceBuilderOptions,
name string,
labels map[string]string,
annotations map[string]string,
ports []corev1.ContainerPort,
) *BaseServiceBuilder {

ports := ContainerPorts2ServicePorts(options.Ports)
servicePorts := ContainerPorts2ServicePorts(ports)

return &BaseServiceBuilder{
BaseResourceBuilder: BaseResourceBuilder{
Client: client,
name: options.Name,
labels: options.Labels,
name: name,
labels: labels,
},
ports: ports,
ports: servicePorts,
}
}
2 changes: 1 addition & 1 deletion pkg/builder/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (b *StatefulSet) GetObject() (*appv1.StatefulSet, error) {
obj := &appv1.StatefulSet{
ObjectMeta: b.GetObjectMeta(),
Spec: appv1.StatefulSetSpec{
Selector: b.GetSelector(),
Selector: b.GetLabelSelector(),
Template: *tpl,
},
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ type Client struct {
OwnerReference ctrlclient.Object
}

// NewClient returns a new instance of the Client struct.
// It accepts a control client `client` and an owner reference `ownerReference`.
func NewClient(client ctrlclient.Client, ownerReference ctrlclient.Object) *Client {
return &Client{
Client: client,
OwnerReference: ownerReference,
}
}

// GetCtrlClient returns the control client associated with the client.
func (c *Client) GetCtrlClient() ctrlclient.Client {
return c.Client
Expand Down
Loading

0 comments on commit c221b91

Please sign in to comment.