Skip to content

Commit 3b01147

Browse files
authored
Merge pull request #320 from justinsb/deployments_should_go_ready
mockkubeapiserver: deployments should become healthy
2 parents c4cb85f + 3eab8a5 commit 3b01147

File tree

7 files changed

+160
-38
lines changed

7 files changed

+160
-38
lines changed

applylib/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ go 1.19
88

99
require (
1010
github.com/google/go-cmp v0.5.9
11-
k8s.io/api v0.26.0
11+
k8s.io/api v0.26.2
1212
k8s.io/apimachinery v0.26.2
13-
k8s.io/client-go v0.26.0
13+
k8s.io/client-go v0.26.2
1414
k8s.io/klog/v2 v2.90.1
1515
sigs.k8s.io/controller-runtime v0.14.1
1616
sigs.k8s.io/kubebuilder-declarative-pattern/mockkubeapiserver v0.0.0-20221021151406-9bd3fb842119

applylib/go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,13 +483,13 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
483483
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
484484
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
485485
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
486-
k8s.io/api v0.26.0 h1:IpPlZnxBpV1xl7TGk/X6lFtpgjgntCg8PJ+qrPHAC7I=
487-
k8s.io/api v0.26.0/go.mod h1:k6HDTaIFC8yn1i6pSClSqIwLABIcLV9l5Q4EcngKnQg=
486+
k8s.io/api v0.26.2 h1:dM3cinp3PGB6asOySalOZxEG4CZ0IAdJsrYZXE/ovGQ=
487+
k8s.io/api v0.26.2/go.mod h1:1kjMQsFE+QHPfskEcVNgL3+Hp88B80uj0QtSOlj8itU=
488488
k8s.io/apiextensions-apiserver v0.26.0 h1:Gy93Xo1eg2ZIkNX/8vy5xviVSxwQulsnUdQ00nEdpDo=
489489
k8s.io/apimachinery v0.26.2 h1:da1u3D5wfR5u2RpLhE/ZtZS2P7QvDgLZTi9wrNZl/tQ=
490490
k8s.io/apimachinery v0.26.2/go.mod h1:ats7nN1LExKHvJ9TmwootT00Yz05MuYqPXEXaVeOy5I=
491-
k8s.io/client-go v0.26.0 h1:lT1D3OfO+wIi9UFolCrifbjUUgu7CpLca0AD8ghRLI8=
492-
k8s.io/client-go v0.26.0/go.mod h1:I2Sh57A79EQsDmn7F7ASpmru1cceh3ocVT9KlX2jEZg=
491+
k8s.io/client-go v0.26.2 h1:s1WkVujHX3kTp4Zn4yGNFK+dlDXy1bAAkIl+cFAiuYI=
492+
k8s.io/client-go v0.26.2/go.mod h1:u5EjOuSyBa09yqqyY7m3abZeovO/7D/WehVVlZ2qcqU=
493493
k8s.io/klog/v2 v2.90.1 h1:m4bYOKall2MmOiRaR1J+We67Do7vm9KiQVlT96lnHUw=
494494
k8s.io/klog/v2 v2.90.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
495495
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 h1:+70TFaan3hfJzs+7VK2o+OGxg8HsuBr/5f6tVAjDu6E=

dev/test

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
# Copyright 2022 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
# CI script to run all the test commands
19+
20+
set -o errexit
21+
set -o nounset
22+
set -o pipefail
23+
24+
# cd to the repo root
25+
REPO_ROOT=$(git rev-parse --show-toplevel)
26+
cd "${REPO_ROOT}"
27+
28+
set -x
29+
30+
pushd mockkubeapiserver
31+
CGO_ENABLED=0 go test -count=1 -v ./...
32+
popd
33+
34+
pushd applylib
35+
CGO_ENABLED=0 go test -count=1 -v ./...
36+
popd
37+
38+
CGO_ENABLED=0 go test -count=1 -v ./...
39+
40+
pushd examples/guestbook-operator
41+
CGO_ENABLED=0 go test -count=1 -v ./...
42+
popd

hack/ci/test.sh

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ chmod +x bin/kubectl
3737
export PATH="${REPO_ROOT}/bin:$PATH"
3838
echo "kubectl version is $(kubectl version --client)"
3939

40-
export GO111MODULE=on
41-
42-
go test -v -count=1 sigs.k8s.io/kubebuilder-declarative-pattern/pkg/...
43-
44-
cd examples/guestbook-operator
45-
go test sigs.k8s.io/kubebuilder-declarative-pattern/examples/guestbook-operator/controllers/...
40+
# Run with a go workspace
41+
rm -f go.work go.work.sum
42+
go work init .
43+
go work use applylib
44+
go work use mockkubeapiserver
45+
go work use examples/guestbook-operator
46+
47+
dev/test

mockkubeapiserver/controllers.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func (s *MemoryStorage) objectChanged(u *unstructured.Unstructured) {
3232
switch gvk.GroupKind() {
3333
case schema.GroupKind{Kind: "Namespace"}:
3434
s.namespaceChanged(u)
35+
case schema.GroupKind{Group: "apps", Kind: "Deployment"}:
36+
if err := s.deploymentChanged(u); err != nil {
37+
klog.Fatalf("could not update deployment status: %v", err)
38+
}
3539
case schema.GroupKind{Group: "apiextensions.k8s.io", Kind: "CustomResourceDefinition"}:
3640
if err := s.crdChanged(u); err != nil {
3741
klog.Warningf("crd change was invalid: %v", err)
@@ -150,3 +154,60 @@ func (s *MemoryStorage) crdChanged(u *unstructured.Unstructured) error {
150154
}
151155
return nil
152156
}
157+
158+
func (s *MemoryStorage) deploymentChanged(u *unstructured.Unstructured) error {
159+
// So that deployments become ready, we immediately update the status.
160+
// We could do something better here, like e.g. a 1 second pause before changing the status
161+
statusObj := u.Object["status"]
162+
if statusObj == nil {
163+
statusObj = make(map[string]interface{})
164+
u.Object["status"] = statusObj
165+
}
166+
status, ok := statusObj.(map[string]interface{})
167+
if !ok {
168+
return fmt.Errorf("status was of unexpected type %T", statusObj)
169+
}
170+
171+
generation := u.GetGeneration()
172+
if generation == 0 {
173+
generation = 1
174+
u.SetGeneration(generation)
175+
}
176+
177+
replicasVal, _, err := unstructured.NestedFieldNoCopy(u.Object, "spec", "replicas")
178+
if err != nil {
179+
return fmt.Errorf("error getting spec.replicas: %w", err)
180+
}
181+
replicas := int64(0)
182+
switch replicasVal := replicasVal.(type) {
183+
case int64:
184+
replicas = replicasVal
185+
case float64:
186+
replicas = int64(replicasVal)
187+
default:
188+
return fmt.Errorf("unhandled type for spec.replicas %T", replicasVal)
189+
}
190+
191+
var conditions []interface{}
192+
conditions = append(conditions, map[string]interface{}{
193+
"type": "Available",
194+
"status": "True",
195+
"reason": "MinimumReplicasAvailable",
196+
})
197+
conditions = append(conditions, map[string]interface{}{
198+
"type": "Progressing",
199+
"status": "True",
200+
"reason": "NewReplicaSetAvailable",
201+
})
202+
status["conditions"] = conditions
203+
204+
status["availableReplicas"] = replicas
205+
status["readyReplicas"] = replicas
206+
status["replicas"] = replicas
207+
status["updatedReplicas"] = replicas
208+
209+
observedGeneration := generation
210+
status["observedGeneration"] = observedGeneration
211+
212+
return nil
213+
}

0 commit comments

Comments
 (0)