2021-03-01 21:41:55 +00:00
|
|
|
// Copyright 2021 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2021-05-20 17:26:07 +00:00
|
|
|
// Package issuerconfig contains helpers for updating CredentialIssuer status entries.
|
2021-03-01 21:41:55 +00:00
|
|
|
package issuerconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-19 21:53:00 +00:00
|
|
|
"fmt"
|
2021-03-01 21:41:55 +00:00
|
|
|
"sort"
|
|
|
|
|
2021-05-19 21:53:00 +00:00
|
|
|
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
2021-03-01 21:41:55 +00:00
|
|
|
"go.pinniped.dev/generated/latest/apis/concierge/config/v1alpha1"
|
|
|
|
"go.pinniped.dev/generated/latest/client/concierge/clientset/versioned"
|
|
|
|
)
|
|
|
|
|
2021-05-19 21:53:00 +00:00
|
|
|
// Update a strategy on an existing CredentialIssuer, merging into any existing strategy entries.
|
|
|
|
func Update(ctx context.Context, client versioned.Interface, issuer *v1alpha1.CredentialIssuer, strategy v1alpha1.CredentialIssuerStrategy) error {
|
|
|
|
// Update the existing object to merge in the new strategy.
|
|
|
|
updated := issuer.DeepCopy()
|
|
|
|
mergeStrategy(&updated.Status, strategy)
|
|
|
|
|
|
|
|
// If the status has not changed, we're done.
|
|
|
|
if apiequality.Semantic.DeepEqual(issuer.Status, updated.Status) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := client.ConfigV1alpha1().CredentialIssuers().UpdateStatus(ctx, updated, metav1.UpdateOptions{}); err != nil {
|
|
|
|
return fmt.Errorf("failed to update CredentialIssuer status: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-01 21:41:55 +00:00
|
|
|
func mergeStrategy(configToUpdate *v1alpha1.CredentialIssuerStatus, strategy v1alpha1.CredentialIssuerStrategy) {
|
|
|
|
var existing *v1alpha1.CredentialIssuerStrategy
|
|
|
|
for i := range configToUpdate.Strategies {
|
|
|
|
if configToUpdate.Strategies[i].Type == strategy.Type {
|
|
|
|
existing = &configToUpdate.Strategies[i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if existing != nil {
|
2021-05-27 22:09:12 +00:00
|
|
|
if !equalExceptLastUpdated(existing, &strategy) {
|
|
|
|
strategy.DeepCopyInto(existing)
|
|
|
|
}
|
2021-03-01 21:41:55 +00:00
|
|
|
} else {
|
|
|
|
configToUpdate.Strategies = append(configToUpdate.Strategies, strategy)
|
|
|
|
}
|
|
|
|
sort.Stable(sortableStrategies(configToUpdate.Strategies))
|
2021-03-02 18:55:24 +00:00
|
|
|
|
|
|
|
// Special case: the "TokenCredentialRequestAPI" data is mirrored into the deprecated status.kubeConfigInfo field.
|
|
|
|
if strategy.Frontend != nil && strategy.Frontend.Type == v1alpha1.TokenCredentialRequestAPIFrontendType {
|
|
|
|
configToUpdate.KubeConfigInfo = &v1alpha1.CredentialIssuerKubeConfigInfo{
|
|
|
|
Server: strategy.Frontend.TokenCredentialRequestAPIInfo.Server,
|
|
|
|
CertificateAuthorityData: strategy.Frontend.TokenCredentialRequestAPIInfo.CertificateAuthorityData,
|
|
|
|
}
|
|
|
|
}
|
2021-03-01 21:41:55 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 20:03:27 +00:00
|
|
|
// weights are a set of priorities for each strategy type.
|
|
|
|
//nolint: gochecknoglobals
|
|
|
|
var weights = map[v1alpha1.StrategyType]int{
|
|
|
|
v1alpha1.KubeClusterSigningCertificateStrategyType: 2, // most preferred strategy
|
|
|
|
v1alpha1.ImpersonationProxyStrategyType: 1,
|
|
|
|
// unknown strategy types will have weight 0 by default
|
|
|
|
}
|
|
|
|
|
2021-03-01 21:41:55 +00:00
|
|
|
type sortableStrategies []v1alpha1.CredentialIssuerStrategy
|
|
|
|
|
2021-03-03 20:03:27 +00:00
|
|
|
func (s sortableStrategies) Len() int { return len(s) }
|
|
|
|
func (s sortableStrategies) Less(i, j int) bool {
|
|
|
|
if wi, wj := weights[s[i].Type], weights[s[j].Type]; wi != wj {
|
|
|
|
return wi > wj
|
|
|
|
}
|
|
|
|
return s[i].Type < s[j].Type
|
|
|
|
}
|
|
|
|
func (s sortableStrategies) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
2021-05-27 22:09:12 +00:00
|
|
|
|
|
|
|
func equalExceptLastUpdated(s1, s2 *v1alpha1.CredentialIssuerStrategy) bool {
|
|
|
|
s1 = s1.DeepCopy()
|
|
|
|
s2 = s2.DeepCopy()
|
|
|
|
s1.LastUpdateTime = metav1.Time{}
|
|
|
|
s2.LastUpdateTime = metav1.Time{}
|
|
|
|
return apiequality.Semantic.DeepEqual(s1, s2)
|
|
|
|
}
|