Files
capsule/pkg/webhook/pod/imagepullpolicy_pullpolicy.go
2021-07-02 10:14:06 +02:00

49 lines
1.0 KiB
Go

// Copyright 2020-2021 Clastix Labs
// SPDX-License-Identifier: Apache-2.0
package pod
import (
"strings"
capsulev1beta1 "github.com/clastix/capsule/api/v1beta1"
)
type PullPolicy interface {
IsPolicySupported(policy string) bool
AllowedPullPolicies() []string
}
type imagePullPolicyValidator struct {
allowedPolicies []string
}
func (i imagePullPolicyValidator) IsPolicySupported(policy string) bool {
for _, allowed := range i.allowedPolicies {
if strings.EqualFold(allowed, policy) {
return true
}
}
return false
}
func (i imagePullPolicyValidator) AllowedPullPolicies() []string {
return i.allowedPolicies
}
func NewPullPolicy(tenant *capsulev1beta1.Tenant) PullPolicy {
// the Tenant doesn't enforce the allowed image pull policy, returning nil
if len(tenant.Spec.ImagePullPolicies) == 0 {
return nil
}
var allowedPolicies []string
for _, policy := range tenant.Spec.ImagePullPolicies {
allowedPolicies = append(allowedPolicies, policy.String())
}
return &imagePullPolicyValidator{
allowedPolicies: allowedPolicies,
}
}