forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cruds.go
113 lines (90 loc) · 3.47 KB
/
cruds.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
/*
Copyright 2018 The Knative Authors.
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 revision
import (
"context"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/equality"
caching "knative.dev/caching/pkg/apis/caching/v1alpha1"
"knative.dev/pkg/kmp"
"knative.dev/pkg/logging"
av1alpha1 "knative.dev/serving/pkg/apis/autoscaling/v1alpha1"
"knative.dev/serving/pkg/apis/serving/v1alpha1"
"knative.dev/serving/pkg/reconciler/revision/config"
"knative.dev/serving/pkg/reconciler/revision/resources"
presources "knative.dev/serving/pkg/resources"
)
func (c *Reconciler) createDeployment(ctx context.Context, rev *v1alpha1.Revision) (*appsv1.Deployment, error) {
cfgs := config.FromContext(ctx)
deployment := resources.MakeDeployment(
rev,
cfgs.Logging,
cfgs.Tracing,
cfgs.Network,
cfgs.Observability,
cfgs.Autoscaler,
cfgs.Deployment,
)
return c.KubeClientSet.AppsV1().Deployments(deployment.Namespace).Create(deployment)
}
func (c *Reconciler) checkAndUpdateDeployment(ctx context.Context, rev *v1alpha1.Revision, have *appsv1.Deployment) (*appsv1.Deployment, error) {
logger := logging.FromContext(ctx)
cfgs := config.FromContext(ctx)
deployment := resources.MakeDeployment(
rev,
cfgs.Logging,
cfgs.Tracing,
cfgs.Network,
cfgs.Observability,
cfgs.Autoscaler,
cfgs.Deployment,
)
// Preserve the current scale of the Deployment.
deployment.Spec.Replicas = have.Spec.Replicas
// Preserve the label selector since it's immutable.
// TODO(dprotaso): determine other immutable properties.
deployment.Spec.Selector = have.Spec.Selector
// If the spec we want is the spec we have, then we're good.
if equality.Semantic.DeepEqual(have.Spec, deployment.Spec) {
return have, nil
}
// Otherwise attempt an update (with ONLY the spec changes).
desiredDeployment := have.DeepCopy()
desiredDeployment.Spec = deployment.Spec
// Carry over new labels.
desiredDeployment.Labels = presources.UnionMaps(deployment.Labels, desiredDeployment.Labels)
d, err := c.KubeClientSet.AppsV1().Deployments(deployment.Namespace).Update(desiredDeployment)
if err != nil {
return nil, err
}
// If what comes back from the update (with defaults applied by the API server) is the same
// as what we have then nothing changed.
if equality.Semantic.DeepEqual(have.Spec, d.Spec) {
return d, nil
}
diff, err := kmp.SafeDiff(have.Spec, d.Spec)
if err != nil {
return nil, err
}
// If what comes back has a different spec, then signal the change.
logger.Infof("Reconciled deployment diff (-desired, +observed): %v", diff)
return d, nil
}
func (c *Reconciler) createImageCache(ctx context.Context, rev *v1alpha1.Revision) (*caching.Image, error) {
image := resources.MakeImageCache(rev)
return c.CachingClientSet.CachingV1alpha1().Images(image.Namespace).Create(image)
}
func (c *Reconciler) createPA(ctx context.Context, rev *v1alpha1.Revision) (*av1alpha1.PodAutoscaler, error) {
pa := resources.MakePA(rev)
return c.ServingClientSet.AutoscalingV1alpha1().PodAutoscalers(pa.Namespace).Create(pa)
}