|
| 1 | +/* |
| 2 | +Copyright 2020 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 storageversion |
| 18 | + |
| 19 | +import ( |
| 20 | + "sync" |
| 21 | + "sync/atomic" |
| 22 | + |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/apimachinery/pkg/runtime" |
| 25 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 26 | + apiserverclientset "k8s.io/apiserver/pkg/client/clientset_generated/clientset" |
| 27 | + "k8s.io/client-go/rest" |
| 28 | + _ "k8s.io/component-base/metrics/prometheus/workqueue" // for workqueue metric registration |
| 29 | + "k8s.io/klog" |
| 30 | +) |
| 31 | + |
| 32 | +// ResourceInfo contains the information to register the resource to the |
| 33 | +// storage version API. |
| 34 | +type ResourceInfo struct { |
| 35 | + Resource metav1.APIResource |
| 36 | + // We use a standalone Group instead of reusing the Resource.Group |
| 37 | + // because Resource.Group is often omitted, see the comment on |
| 38 | + // Resource.Group for why it's omitted. |
| 39 | + Group string |
| 40 | + EncodingVersion string |
| 41 | + DecodableVersions []string |
| 42 | + EquivalentResourceMapper runtime.EquivalentResourceRegistry |
| 43 | +} |
| 44 | + |
| 45 | +// Manager records the resources whose StorageVersions need updates, and provides a method to update those StorageVersions. |
| 46 | +type Manager interface { |
| 47 | + // AddResourceInfo adds ResourceInfo to the manager. |
| 48 | + AddResourceInfo(resources ...*ResourceInfo) |
| 49 | + // RemoveResourceInfo removes ResourceInfo from the manager. |
| 50 | + RemoveResourceInfo(r *ResourceInfo) |
| 51 | + // UpdatesPending returns if the StorageVersion of a resource is still wait to be updated. |
| 52 | + UpdatesPending(group, resource string) bool |
| 53 | + |
| 54 | + // UpdateStorageVersions updates the StorageVersions. |
| 55 | + UpdateStorageVersions(loopbackClientConfig *rest.Config, apiserverID string) |
| 56 | + // Completed returns if updating StorageVersions has completed. |
| 57 | + Completed() bool |
| 58 | +} |
| 59 | + |
| 60 | +var _ Manager = &DefaultManager{} |
| 61 | + |
| 62 | +// NewDefaultManager creates a new DefaultManager. |
| 63 | +func NewDefaultManager() *DefaultManager { |
| 64 | + s := &DefaultManager{} |
| 65 | + s.completed.Store(false) |
| 66 | + s.groupResources = make(map[string]map[string]struct{}) |
| 67 | + s.resources = make(map[*ResourceInfo]struct{}) |
| 68 | + return s |
| 69 | +} |
| 70 | + |
| 71 | +// AddResourceInfo adds ResourceInfo to the manager. |
| 72 | +// This is not thread-safe. It is expected to be called when the apiserver is installing the endpoints, which is done serially. |
| 73 | +func (s *DefaultManager) AddResourceInfo(resources ...*ResourceInfo) { |
| 74 | + for _, r := range resources { |
| 75 | + s.resources[r] = struct{}{} |
| 76 | + s.addGroupResourceFor(r) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func (s *DefaultManager) addGroupResourceFor(r *ResourceInfo) { |
| 81 | + gvrs := r.EquivalentResourceMapper.EquivalentResourcesFor(schema.GroupVersionResource{ |
| 82 | + Group: r.Group, |
| 83 | + Resource: r.Resource.Name, |
| 84 | + }, "") |
| 85 | + for _, gvr := range gvrs { |
| 86 | + s.addGroupResource(gvr.Group, gvr.Resource) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func (s *DefaultManager) addGroupResource(group, resource string) { |
| 91 | + if _, ok := s.groupResources[group]; !ok { |
| 92 | + s.groupResources[group] = make(map[string]struct{}) |
| 93 | + } |
| 94 | + s.groupResources[group][resource] = struct{}{} |
| 95 | +} |
| 96 | + |
| 97 | +// RemoveResourceInfo removes ResourceInfo from the manager. |
| 98 | +// It is not safe to call this function concurrently with AddResourceInfo. |
| 99 | +func (s *DefaultManager) RemoveResourceInfo(r *ResourceInfo) { |
| 100 | + s.mu.Lock() |
| 101 | + defer s.mu.Unlock() |
| 102 | + delete(s.resources, r) |
| 103 | + s.removeGroupResourceFor(r) |
| 104 | +} |
| 105 | + |
| 106 | +func (s *DefaultManager) removeGroupResourceFor(r *ResourceInfo) { |
| 107 | + gvrs := r.EquivalentResourceMapper.EquivalentResourcesFor(schema.GroupVersionResource{ |
| 108 | + Group: r.Group, |
| 109 | + Resource: r.Resource.Name, |
| 110 | + }, "") |
| 111 | + for _, gvr := range gvrs { |
| 112 | + s.removeGroupResource(gvr.Group, gvr.Version) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func (s *DefaultManager) removeGroupResource(group, resource string) { |
| 117 | + if _, ok := s.groupResources[group]; !ok { |
| 118 | + return |
| 119 | + } |
| 120 | + delete(s.groupResources[group], resource) |
| 121 | + if len(s.groupResources[group]) == 0 { |
| 122 | + delete(s.groupResources, group) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// UpdatesPending returns if the StorageVersion of a resource is still wait to be updated. |
| 127 | +func (s *DefaultManager) UpdatesPending(group, resource string) bool { |
| 128 | + s.mu.RLock() |
| 129 | + defer s.mu.RUnlock() |
| 130 | + if _, ok := s.groupResources[group]; !ok { |
| 131 | + return false |
| 132 | + } |
| 133 | + _, ok := s.groupResources[group][resource] |
| 134 | + return ok |
| 135 | +} |
| 136 | + |
| 137 | +// DefaultManager indicates if the aggregator, kube-apiserver, and the |
| 138 | +// apiextensions-apiserver have completed reporting their storage versions. |
| 139 | +type DefaultManager struct { |
| 140 | + completed atomic.Value |
| 141 | + |
| 142 | + mu sync.RWMutex |
| 143 | + resources map[*ResourceInfo]struct{} |
| 144 | + groupResources map[string]map[string]struct{} |
| 145 | +} |
| 146 | + |
| 147 | +// setComplete marks the completion of updating StorageVersions. No write requests need to be blocked anymore. |
| 148 | +func (s *DefaultManager) setComplete() { |
| 149 | + s.completed.Store(true) |
| 150 | +} |
| 151 | + |
| 152 | +// Completed returns if updating StorageVersions has completed. |
| 153 | +func (s *DefaultManager) Completed() bool { |
| 154 | + return s.completed.Load().(bool) |
| 155 | +} |
| 156 | + |
| 157 | +func decodableVersions(e runtime.EquivalentResourceRegistry, group string, resource string) []string { |
| 158 | + var versions []string |
| 159 | + decodingGVRs := e.EquivalentResourcesFor(schema.GroupVersionResource{ |
| 160 | + Group: group, |
| 161 | + Resource: resource, |
| 162 | + }, "") |
| 163 | + for _, v := range decodingGVRs { |
| 164 | + versions = append(versions, v.GroupVersion().String()) |
| 165 | + } |
| 166 | + return versions |
| 167 | +} |
| 168 | + |
| 169 | +// UpdateStorageVersions updates the StorageVersions. If the updates are |
| 170 | +// successful, following calls to Completed() returns true. |
| 171 | +func (s *DefaultManager) UpdateStorageVersions(loopbackClientConfig *rest.Config, serverID string) { |
| 172 | + cfg := rest.AddUserAgent(loopbackClientConfig, "system:kube-apiserver") |
| 173 | + clientset, err := apiserverclientset.NewForConfig(cfg) |
| 174 | + if err != nil { |
| 175 | + klog.Fatalf("failed to get clientset: %v", err) |
| 176 | + return |
| 177 | + } |
| 178 | + sc := clientset.InternalV1alpha1().StorageVersions() |
| 179 | + |
| 180 | + s.mu.RLock() |
| 181 | + resources := s.resources |
| 182 | + s.mu.RUnlock() |
| 183 | + for r := range resources { |
| 184 | + r.DecodableVersions = decodableVersions(r.EquivalentResourceMapper, r.Group, r.Resource.Name) |
| 185 | + if err := updateStorageVersionFor(sc, serverID, r.Group+"."+r.Resource.Name, r.EncodingVersion, r.DecodableVersions); err != nil { |
| 186 | + klog.Fatalf("failed to update storage version for %v", r.Resource.Name) |
| 187 | + return |
| 188 | + } |
| 189 | + klog.V(2).Infof("successfully updated storage version for %v", r.Resource.Name) |
| 190 | + s.RemoveResourceInfo(r) |
| 191 | + } |
| 192 | + klog.V(2).Infof("storage version updates complete") |
| 193 | + s.setComplete() |
| 194 | +} |
0 commit comments