forked from knative/serving
/
labels.go
115 lines (100 loc) · 3.63 KB
/
labels.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
/*
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
https://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 route
import (
"context"
"errors"
"fmt"
"sort"
"github.com/knative/serving/pkg/apis/serving"
"github.com/knative/serving/pkg/apis/serving/v1alpha1"
"github.com/knative/serving/pkg/controller/route/traffic"
"github.com/knative/serving/pkg/logging"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
)
func (c *Controller) syncLabels(ctx context.Context, r *v1alpha1.Route, tc *traffic.TrafficConfig) error {
if err := c.deleteLabelForOutsideOfGivenConfigurations(ctx, r, tc.Configurations); err != nil {
return err
}
if err := c.setLabelForGivenConfigurations(ctx, r, tc.Configurations); err != nil {
return err
}
return nil
}
func (c *Controller) setLabelForGivenConfigurations(
ctx context.Context, route *v1alpha1.Route, configMap map[string]*v1alpha1.Configuration) error {
logger := logging.FromContext(ctx)
configClient := c.ServingClientSet.ServingV1alpha1().Configurations(route.Namespace)
names := []string{}
// Validate
for _, config := range configMap {
names = append(names, config.Name)
routeName, ok := config.Labels[serving.RouteLabelKey]
if !ok {
continue
}
// TODO(yanweiguo): add a condition in status for this error
if routeName != route.Name {
errMsg := fmt.Sprintf("Configuration %q is already in use by %q, and cannot be used by %q",
config.Name, routeName, route.Name)
c.Recorder.Event(route, corev1.EventTypeWarning, "ConfigurationInUse", errMsg)
logger.Error(errMsg)
return errors.New(errMsg)
}
}
// Sort the names to give things a deterministic ordering.
sort.Strings(names)
// Set label for newly added configurations as traffic target.
for _, configName := range names {
config := configMap[configName]
if config.Labels == nil {
config.Labels = make(map[string]string)
} else if _, ok := config.Labels[serving.RouteLabelKey]; ok {
continue
}
config.Labels[serving.RouteLabelKey] = route.Name
if _, err := configClient.Update(config); err != nil {
logger.Errorf("Failed to update Configuration %s: %s", config.Name, err)
return err
}
}
return nil
}
func (c *Controller) deleteLabelForOutsideOfGivenConfigurations(
ctx context.Context, route *v1alpha1.Route, configMap map[string]*v1alpha1.Configuration) error {
logger := logging.FromContext(ctx)
ns := route.Namespace
// Get Configurations set as traffic target before this sync.
selector, err := labels.Parse(fmt.Sprintf("%s=%s", serving.RouteLabelKey, route.Name))
if err != nil {
return err
}
oldConfigsList, err := c.configurationLister.Configurations(ns).List(selector)
if err != nil {
logger.Errorf("Failed to fetch configurations with label '%s=%s': %s",
serving.RouteLabelKey, route.Name, err)
return err
}
// Delete label for newly removed configurations as traffic target.
for _, config := range oldConfigsList {
if _, ok := configMap[config.Name]; !ok {
delete(config.Labels, serving.RouteLabelKey)
if _, err := c.ServingClientSet.ServingV1alpha1().Configurations(ns).Update(config); err != nil {
logger.Errorf("Failed to update Configuration %s: %s", config.Name, err)
return err
}
}
}
return nil
}