2020-09-16 14:19:51 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-07-16 16:40:44 +00:00
|
|
|
|
2020-08-11 17:14:57 +00:00
|
|
|
package apicerts
|
2020-07-16 16:40:44 +00:00
|
|
|
|
|
|
|
import (
|
2020-09-08 23:36:49 +00:00
|
|
|
"bytes"
|
2020-07-16 16:40:44 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/client-go/util/retry"
|
2020-08-09 17:04:05 +00:00
|
|
|
aggregatorclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
|
2020-08-04 23:46:27 +00:00
|
|
|
)
|
2020-07-16 16:40:44 +00:00
|
|
|
|
2020-08-04 23:46:27 +00:00
|
|
|
// UpdateAPIService updates the APIService's CA bundle.
|
2020-11-11 22:49:13 +00:00
|
|
|
func UpdateAPIService(ctx context.Context, aggregatorClient aggregatorclient.Interface, apiServiceName, serviceNamespace string, aggregatedAPIServerCA []byte) error {
|
2020-08-09 17:04:05 +00:00
|
|
|
apiServices := aggregatorClient.ApiregistrationV1().APIServices()
|
2020-07-16 16:40:44 +00:00
|
|
|
|
|
|
|
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
2020-08-28 00:11:10 +00:00
|
|
|
// Retrieve the latest version of the Service.
|
2020-08-04 23:46:27 +00:00
|
|
|
fetchedAPIService, err := apiServices.Get(ctx, apiServiceName, metav1.GetOptions{})
|
2020-07-16 16:40:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get existing version of API service: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-11 22:49:13 +00:00
|
|
|
if serviceRef := fetchedAPIService.Spec.Service; serviceRef != nil {
|
|
|
|
if serviceRef.Namespace != serviceNamespace {
|
|
|
|
// we do not own this API service so do not attempt to mutate it
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 23:36:49 +00:00
|
|
|
if bytes.Equal(fetchedAPIService.Spec.CABundle, aggregatedAPIServerCA) {
|
|
|
|
// Already has the same value, perhaps because another process already updated the object, so no need to update.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-04 23:46:27 +00:00
|
|
|
// Update just the field we care about.
|
|
|
|
fetchedAPIService.Spec.CABundle = aggregatedAPIServerCA
|
2020-07-16 16:40:44 +00:00
|
|
|
|
2020-08-04 23:46:27 +00:00
|
|
|
_, updateErr := apiServices.Update(ctx, fetchedAPIService, metav1.UpdateOptions{})
|
2020-07-16 16:40:44 +00:00
|
|
|
return updateErr
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("could not update API service: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|