Skip to content

Commit 11e3221

Browse files
authored
refactor: reduce usage of k8s.io/kubernetes packages (argoproj#258)
Signed-off-by: Mikhail Mazurskiy <[email protected]>
1 parent 8e19104 commit 11e3221

File tree

5 files changed

+131
-3
lines changed

5 files changed

+131
-3
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/argoproj/gitops-engine
33
go 1.15
44

55
require (
6+
github.com/davecgh/go-spew v1.1.1
67
github.com/evanphx/json-patch v4.9.0+incompatible
78
github.com/go-logr/logr v0.3.0
89
github.com/golang/mock v1.4.4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Copyright 2015 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package endpoints
18+
19+
import (
20+
"bytes"
21+
"crypto/md5"
22+
"hash"
23+
"sort"
24+
25+
hashutil "github.com/argoproj/gitops-engine/internal/kubernetes_vendor/pkg/util/hash"
26+
v1 "k8s.io/api/core/v1"
27+
)
28+
29+
// LessEndpointAddress compares IP addresses lexicographically and returns true if first argument is lesser than second
30+
func LessEndpointAddress(a, b *v1.EndpointAddress) bool {
31+
ipComparison := bytes.Compare([]byte(a.IP), []byte(b.IP))
32+
if ipComparison != 0 {
33+
return ipComparison < 0
34+
}
35+
if b.TargetRef == nil {
36+
return false
37+
}
38+
if a.TargetRef == nil {
39+
return true
40+
}
41+
return a.TargetRef.UID < b.TargetRef.UID
42+
}
43+
44+
// SortSubsets sorts an array of EndpointSubset objects in place. For ease of
45+
// use it returns the input slice.
46+
func SortSubsets(subsets []v1.EndpointSubset) []v1.EndpointSubset {
47+
for i := range subsets {
48+
ss := &subsets[i]
49+
sort.Sort(addrsByIPAndUID(ss.Addresses))
50+
sort.Sort(addrsByIPAndUID(ss.NotReadyAddresses))
51+
sort.Sort(portsByHash(ss.Ports))
52+
}
53+
sort.Sort(subsetsByHash(subsets))
54+
return subsets
55+
}
56+
57+
func hashObject(hasher hash.Hash, obj interface{}) []byte {
58+
hashutil.DeepHashObject(hasher, obj)
59+
return hasher.Sum(nil)
60+
}
61+
62+
type subsetsByHash []v1.EndpointSubset
63+
64+
func (sl subsetsByHash) Len() int { return len(sl) }
65+
func (sl subsetsByHash) Swap(i, j int) { sl[i], sl[j] = sl[j], sl[i] }
66+
func (sl subsetsByHash) Less(i, j int) bool {
67+
hasher := md5.New()
68+
h1 := hashObject(hasher, sl[i])
69+
h2 := hashObject(hasher, sl[j])
70+
return bytes.Compare(h1, h2) < 0
71+
}
72+
73+
type addrsByIPAndUID []v1.EndpointAddress
74+
75+
func (sl addrsByIPAndUID) Len() int { return len(sl) }
76+
func (sl addrsByIPAndUID) Swap(i, j int) { sl[i], sl[j] = sl[j], sl[i] }
77+
func (sl addrsByIPAndUID) Less(i, j int) bool {
78+
return LessEndpointAddress(&sl[i], &sl[j])
79+
}
80+
81+
type portsByHash []v1.EndpointPort
82+
83+
func (sl portsByHash) Len() int { return len(sl) }
84+
func (sl portsByHash) Swap(i, j int) { sl[i], sl[j] = sl[j], sl[i] }
85+
func (sl portsByHash) Less(i, j int) bool {
86+
hasher := md5.New()
87+
h1 := hashObject(hasher, sl[i])
88+
h2 := hashObject(hasher, sl[j])
89+
return bytes.Compare(h1, h2) < 0
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2015 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package hash
18+
19+
import (
20+
"hash"
21+
22+
"github.com/davecgh/go-spew/spew"
23+
)
24+
25+
// DeepHashObject writes specified object to hash using the spew library
26+
// which follows pointers and prints actual values of the nested objects
27+
// ensuring the hash does not change when a pointer changes.
28+
func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) {
29+
hasher.Reset()
30+
printer := spew.ConfigState{
31+
Indent: " ",
32+
SortKeys: true,
33+
DisableMethods: true,
34+
SpewKeys: true,
35+
}
36+
printer.Fprintf(hasher, "%#v", objectToWrite)
37+
}

pkg/diff/diff.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818
"k8s.io/apimachinery/pkg/util/jsonmergepatch"
1919
"k8s.io/apimachinery/pkg/util/strategicpatch"
2020
"k8s.io/client-go/kubernetes/scheme"
21-
"k8s.io/kubernetes/pkg/api/v1/endpoints"
2221

22+
"github.com/argoproj/gitops-engine/internal/kubernetes_vendor/pkg/api/v1/endpoints"
2323
jsonutil "github.com/argoproj/gitops-engine/pkg/utils/json"
2424
kubescheme "github.com/argoproj/gitops-engine/pkg/utils/kube/scheme"
2525
)

pkg/health/health_pod.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
corev1 "k8s.io/api/core/v1"
99
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1010
"k8s.io/apimachinery/pkg/runtime"
11-
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
11+
"k8s.io/kubectl/pkg/util/podutils"
1212
)
1313

1414
func getPodHealth(obj *unstructured.Unstructured) (*HealthStatus, error) {
@@ -96,7 +96,7 @@ func getCorev1PodHealth(pod *corev1.Pod) (*HealthStatus, error) {
9696
switch pod.Spec.RestartPolicy {
9797
case corev1.RestartPolicyAlways:
9898
// if pod is ready, it is automatically healthy
99-
if podutil.IsPodReady(pod) {
99+
if podutils.IsPodReady(pod) {
100100
return &HealthStatus{
101101
Status: HealthStatusHealthy,
102102
Message: pod.Status.Message,

0 commit comments

Comments
 (0)