Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resetting service type to default when not specified #8165

Merged
merged 13 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions pkg/controller/common/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,21 @@ func ReconcileResource(params Params) error {
namespace := params.Expected.GetNamespace()
name := params.Expected.GetName()
log := ulog.FromContext(params.Context).WithValues("kind", kind, "namespace", namespace, "name", name)
deleted := false

create := func() error {
if deleted {
// check if not being deleted
err = params.Client.Get(params.Context, types.NamespacedName{Name: name, Namespace: namespace}, params.Reconciled)
if err != nil && !apierrors.IsNotFound(err) {
return err
}
if !params.Reconciled.GetDeletionTimestamp().IsZero() {
log.Info("Waiting for resource to be created because the old one is being deleted")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Waiting" seems a bit misleading to me. With this change it is up to the caller to detect that the resource has actually not been reconciled, and try again later. By "swallowing"/"hiding" the conflict and returning nil here we are assuming that the caller is going to do another attempt, but we can't be sure of that? This makes me feel that it should be up to the caller to decide what to do/log in case of a conflict, by using apierrors.IsAlreadyExists()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, we can't be 100% sure that the caller will retry. You proposal looks a good idea.

This means we will delete and (try) to create until the deletion is effective.

The only thing that tickles me is that with the current log messages it can be a little confusing.

From LoadBalancer to ClusterIP:

Deleting resource as it cannot be updated, it will be recreated
Deleted resource successfully
Creating resource
Deleting resource as it cannot be updated, it will be recreated
Deleted resource successfully
Creating resource
Deleting resource as it cannot be updated, it will be recreated
Deleted resource successfully
Creating resource
Deleting resource as it cannot be updated, it will be recreated
Deleted resource successfully
Creating resource
Deleting resource as it cannot be updated, it will be recreated
Deleted resource successfully
Creating resource
Deleting resource as it cannot be updated, it will be recreated
Deleted resource successfully
Creating resource
Deleting resource as it cannot be updated, it will be recreated
Deleted resource successfully
Creating resource
Creating resource
Created resource successfully
Ensuring no voting exclusions are set
Ensuring no voting exclusions are set
Ensuring no voting exclusions are set

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing that tickles me is that with the current log messages it can be a little confusing.

Is it when we requeue immediately, or when we just ignore the error in the driver? Maybe we could add an additional log in that specific case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is when we ignore the error and then requeue in the driver (at the caller level), see 0218fd6.

return nil
}
}

log.Info("Creating resource")
if params.PreCreate != nil {
if err := params.PreCreate(); err != nil {
Expand Down Expand Up @@ -121,6 +134,12 @@ func ReconcileResource(params Params) error {
return fmt.Errorf("failed to get %s %s/%s: %w", kind, namespace, name, err)
}

// shortcut if being deleted
if !params.Reconciled.GetDeletionTimestamp().IsZero() {
log.Info("Skip reconciliation because the resource is being deleted")
return nil
}

if params.NeedsRecreate != nil && params.NeedsRecreate() {
log.Info("Deleting resource as it cannot be updated, it will be recreated")
reconciledMeta, err := meta.Accessor(params.Reconciled)
Expand All @@ -141,6 +160,7 @@ func ReconcileResource(params Params) error {
return fmt.Errorf("failed to delete %s %s/%s: %w", kind, namespace, name, err)
}
log.Info("Deleted resource successfully")
deleted = true
return create()
}

Expand Down
12 changes: 3 additions & 9 deletions pkg/controller/common/service_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,12 @@ func Test_applyServerSideValues(t *testing.T) {
args: args{
expected: corev1.Service{Spec: corev1.ServiceSpec{}},
reconciled: corev1.Service{Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
ClusterIP: "1.2.3.4",
ClusterIPs: []string{"1.2.3.4"},
SessionAffinity: corev1.ServiceAffinityClientIP,
}},
},
want: corev1.Service{Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
ClusterIP: "1.2.3.4",
ClusterIPs: []string{"1.2.3.4"},
SessionAffinity: corev1.ServiceAffinityClientIP,
Expand All @@ -335,14 +333,12 @@ func Test_applyServerSideValues(t *testing.T) {
args: args{
expected: corev1.Service{Spec: corev1.ServiceSpec{}},
reconciled: corev1.Service{Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
ClusterIP: "None",
ClusterIPs: []string{"None"},
SessionAffinity: corev1.ServiceAffinityClientIP,
}},
},
want: corev1.Service{Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
ClusterIP: "None",
ClusterIPs: []string{"None"},
SessionAffinity: corev1.ServiceAffinityClientIP,
Expand Down Expand Up @@ -531,14 +527,12 @@ func Test_applyServerSideValues(t *testing.T) {
args: args{
expected: corev1.Service{Spec: corev1.ServiceSpec{}},
reconciled: corev1.Service{Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
ClusterIP: "1.2.3.4",
SessionAffinity: corev1.ServiceAffinityClientIP,
IPFamilies: []corev1.IPFamily{corev1.IPv6Protocol},
}},
},
want: corev1.Service{Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
ClusterIP: "1.2.3.4",
SessionAffinity: corev1.ServiceAffinityClientIP,
IPFamilies: []corev1.IPFamily{corev1.IPv6Protocol},
Expand All @@ -551,13 +545,11 @@ func Test_applyServerSideValues(t *testing.T) {
IPFamilies: []corev1.IPFamily{corev1.IPv6Protocol},
}},
reconciled: corev1.Service{Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
ClusterIP: "1.2.3.4",
SessionAffinity: corev1.ServiceAffinityClientIP,
}},
},
want: corev1.Service{Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
ClusterIP: "1.2.3.4",
SessionAffinity: corev1.ServiceAffinityClientIP,
IPFamilies: []corev1.IPFamily{corev1.IPv6Protocol},
Expand Down Expand Up @@ -602,7 +594,9 @@ func Test_applyServerSideValues(t *testing.T) {
{
name: "Reconciled LoadBalancerClass is used if the expected one is empty",
args: args{
expected: corev1.Service{Spec: corev1.ServiceSpec{}},
expected: corev1.Service{Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
}},
reconciled: corev1.Service{Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
LoadBalancerClass: ptr.To("service.k8s.aws/nlb"),
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/elasticsearch/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ func NewExternalService(es esv1.Elasticsearch) *corev1.Service {
svc.ObjectMeta.Namespace = es.Namespace
svc.ObjectMeta.Name = ExternalServiceName(es.Name)

// defaults to ClusterIP if not set
if svc.Spec.Type == "" {
svc.Spec.Type = corev1.ServiceTypeClusterIP
}
labels := label.NewLabels(nsn)
ports := []corev1.ServicePort{
{
Expand Down
23 changes: 13 additions & 10 deletions pkg/controller/elasticsearch/services/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func TestNewExternalService(t *testing.T) {
httpConf commonv1.HTTPConfig
wantSvc func() corev1.Service
}{
{
name: "default clusterIP service",
wantSvc: mkHTTPSService,
},
{
name: "no TLS",
httpConf: commonv1.HTTPConfig{
Expand All @@ -92,11 +96,7 @@ func TestNewExternalService(t *testing.T) {
},
},
},
wantSvc: func() corev1.Service {
svc := mkHTTPService()
svc.Spec.Ports[0].Name = "https"
return svc
},
wantSvc: mkHTTPSService,
},
{
name: "user-provided certificate",
Expand All @@ -107,11 +107,7 @@ func TestNewExternalService(t *testing.T) {
},
},
},
wantSvc: func() corev1.Service {
svc := mkHTTPService()
svc.Spec.Ports[0].Name = "https"
return svc
},
wantSvc: mkHTTPSService,
},
}

Expand Down Expand Up @@ -171,6 +167,7 @@ func mkHTTPService() corev1.Service {
},
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
Ports: []corev1.ServicePort{
{
Name: "http",
Expand All @@ -186,6 +183,12 @@ func mkHTTPService() corev1.Service {
}
}

func mkHTTPSService() corev1.Service {
svc := mkHTTPService()
svc.Spec.Ports[0].Name = "https"
return svc
}

func mkTransportService() corev1.Service {
return corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Expand Down