-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Avinash Patnala <[email protected]>
- Loading branch information
Avinash Patnala
committed
Oct 23, 2024
1 parent
99c98e9
commit eeee348
Showing
3 changed files
with
105 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package transform | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/go-logr/logr" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/kubernetes" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
admissionregistrationv1 "k8s.io/api/admissionregistration/v1" | ||
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1" | ||
rest "k8s.io/client-go/rest" | ||
) | ||
|
||
var vapMux sync.RWMutex | ||
|
||
var VapAPIEnabled *bool | ||
|
||
var GroupVersion *schema.GroupVersion | ||
|
||
func IsVapAPIEnabled(log *logr.Logger) (bool, *schema.GroupVersion) { | ||
vapMux.RLock() | ||
if VapAPIEnabled != nil { | ||
apiEnabled, gvk := *VapAPIEnabled, GroupVersion | ||
vapMux.RUnlock() | ||
return apiEnabled, gvk | ||
} | ||
|
||
vapMux.RUnlock() | ||
vapMux.Lock() | ||
defer vapMux.Unlock() | ||
|
||
if VapAPIEnabled != nil { | ||
return *VapAPIEnabled, GroupVersion | ||
} | ||
config, err := rest.InClusterConfig() | ||
if err != nil { | ||
log.Info("IsVapAPIEnabled InClusterConfig", "error", err) | ||
VapAPIEnabled = new(bool) | ||
*VapAPIEnabled = false | ||
return false, nil | ||
} | ||
clientset, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
log.Info("IsVapAPIEnabled NewForConfig", "error", err) | ||
*VapAPIEnabled = false | ||
return false, nil | ||
} | ||
|
||
groupVersion := admissionregistrationv1.SchemeGroupVersion | ||
resList, err := clientset.Discovery().ServerResourcesForGroupVersion(groupVersion.String()) | ||
if err == nil { | ||
for i := 0; i < len(resList.APIResources); i++ { | ||
if resList.APIResources[i].Name == "validatingadmissionpolicies" { | ||
VapAPIEnabled = new(bool) | ||
*VapAPIEnabled = true | ||
GroupVersion = &groupVersion | ||
return true, GroupVersion | ||
} | ||
} | ||
} | ||
|
||
groupVersion = admissionregistrationv1beta1.SchemeGroupVersion | ||
resList, err = clientset.Discovery().ServerResourcesForGroupVersion(groupVersion.String()) | ||
if err == nil { | ||
for i := 0; i < len(resList.APIResources); i++ { | ||
if resList.APIResources[i].Name == "validatingadmissionpolicies" { | ||
VapAPIEnabled = new(bool) | ||
*VapAPIEnabled = true | ||
GroupVersion = &groupVersion | ||
return true, GroupVersion | ||
} | ||
} | ||
} | ||
|
||
log.Error(err, "error checking VAP API availability", "IsVapAPIEnabled", "false") | ||
VapAPIEnabled = new(bool) | ||
*VapAPIEnabled = false | ||
return false, nil | ||
} | ||
|
||
func VapForVersion(gvk *schema.GroupVersion) (client.Object, error) { | ||
switch gvk.Version { | ||
case "v1": | ||
return &admissionregistrationv1.ValidatingAdmissionPolicy{}, nil | ||
case "v1beta1": | ||
return &admissionregistrationv1beta1.ValidatingAdmissionPolicy{}, nil | ||
default: | ||
return nil, errors.New("unrecognized version") | ||
} | ||
} |