/
service_export_client.go
145 lines (116 loc) · 5.08 KB
/
service_export_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"context"
"reflect"
"github.com/pkg/errors"
"github.com/submariner-io/admiral/pkg/log"
"github.com/submariner-io/admiral/pkg/slices"
"github.com/submariner-io/lighthouse/pkg/constants"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
mcsv1a1 "sigs.k8s.io/mcs-api/pkg/apis/v1alpha1"
)
func (c *ServiceExportClient) removeStatusCondition(ctx context.Context, name, namespace string,
condType mcsv1a1.ServiceExportConditionType, reason string,
) {
c.doUpdate(ctx, name, namespace, func(toUpdate *mcsv1a1.ServiceExport) bool {
condition := FindServiceExportStatusCondition(toUpdate.Status.Conditions, condType)
if condition != nil && reflect.DeepEqual(condition.Reason, &reason) {
logger.V(log.DEBUG).Infof("Removing status condition (Type: %q, Reason: %q) from ServiceExport (%s/%s)",
condType, reason, namespace, name)
toUpdate.Status.Conditions, _ = slices.Remove(toUpdate.Status.Conditions, *condition,
func(c mcsv1a1.ServiceExportCondition) mcsv1a1.ServiceExportConditionType {
return c.Type
})
return true
}
return false
})
}
func (c *ServiceExportClient) updateStatusConditions(ctx context.Context, name, namespace string,
conditions ...mcsv1a1.ServiceExportCondition,
) {
c.tryUpdateStatusConditions(ctx, name, namespace, true, conditions...)
}
func (c *ServiceExportClient) tryUpdateStatusConditions(ctx context.Context, name, namespace string, canReplace bool,
conditions ...mcsv1a1.ServiceExportCondition,
) {
findStatusCondition := func(conditions []mcsv1a1.ServiceExportCondition, condType mcsv1a1.ServiceExportConditionType,
) *mcsv1a1.ServiceExportCondition {
cond := FindServiceExportStatusCondition(conditions, condType)
// TODO - this handles migration of the Synced type to Ready which can be removed once we no longer support a version
// prior to the introduction of Ready.
if cond == nil && condType == constants.ServiceExportReady {
cond = FindServiceExportStatusCondition(conditions, "Synced")
}
return cond
}
c.doUpdate(ctx, name, namespace, func(toUpdate *mcsv1a1.ServiceExport) bool {
updated := false
for i := range conditions {
condition := &conditions[i]
prevCond := findStatusCondition(toUpdate.Status.Conditions, condition.Type)
if prevCond == nil {
logger.V(log.DEBUG).Infof("Add status condition for ServiceExport (%s/%s): Type: %q, Status: %q, Reason: %q, Message: %q",
namespace, name, condition.Type, condition.Status, *condition.Reason, *condition.Message)
toUpdate.Status.Conditions = append(toUpdate.Status.Conditions, *condition)
updated = true
} else if serviceExportConditionEqual(prevCond, condition) {
logger.V(log.TRACE).Infof("Last ServiceExportCondition for (%s/%s) is equal - not updating status: %#v",
namespace, name, prevCond)
} else if canReplace {
logger.V(log.DEBUG).Infof("Update status condition for ServiceExport (%s/%s): Type: %q, Status: %q, Reason: %q, Message: %q",
namespace, name, condition.Type, condition.Status, *condition.Reason, *condition.Message)
*prevCond = *condition
updated = true
}
}
return updated
})
}
func (c *ServiceExportClient) doUpdate(ctx context.Context, name, namespace string, update func(toUpdate *mcsv1a1.ServiceExport) bool) {
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
obj, err := c.Namespace(namespace).Get(ctx, name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
logger.V(log.TRACE).Infof("ServiceExport (%s/%s) not found - unable to update status", namespace, name)
return nil
} else if err != nil {
return errors.Wrap(err, "error retrieving ServiceExport")
}
toUpdate := c.toServiceExport(obj)
updated := update(toUpdate)
if !updated {
return nil
}
_, err = c.Namespace(toUpdate.Namespace).UpdateStatus(ctx, c.toUnstructured(toUpdate), metav1.UpdateOptions{})
return errors.Wrap(err, "error from UpdateStatus")
})
if err != nil {
logger.Errorf(err, "Error updating status for ServiceExport (%s/%s)", namespace, name)
}
}
func (c *ServiceExportClient) getLocalInstance(name, namespace string) *mcsv1a1.ServiceExport {
obj, found, _ := c.localSyncer.GetResource(name, namespace)
if !found {
return nil
}
return obj.(*mcsv1a1.ServiceExport)
}
func serviceExportConditionEqual(c1, c2 *mcsv1a1.ServiceExportCondition) bool {
return c1.Type == c2.Type && c1.Status == c2.Status && reflect.DeepEqual(c1.Reason, c2.Reason) &&
reflect.DeepEqual(c1.Message, c2.Message)
}