Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: tuning for testing #156

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/controller/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func runController(
}

if isGKE {
csrMgr := csr.NewApprovalManager(log, clientset)
csrMgr := csr.NewApprovalManager(log, clientset, cfg.ServiceAccount)
if err := csrMgr.Start(ctx); err != nil {
log.WithError(err).Fatal("failed to start approval manager")
}
Expand Down
11 changes: 7 additions & 4 deletions internal/actions/csr/csr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/pem"
"errors"
"fmt"
"log"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -91,11 +92,11 @@ func (c *Certificate) ForCASTAINode() bool {
return false
}

func (c *Certificate) NodeBootstrap() bool {
func (c *Certificate) NodeBootstrap(serviceAccount string) bool {
// Since we only have one handler per CSR/certificate name,
// which is the node name, we can process the controller's certificates and kubelet-bootstrap`s.
// This covers the case when the controller restarts but the bootstrap certificate was deleted without our own certificate being approved.
return c.RequestingUser == "kubelet-bootstrap" || c.RequestingUser == "system:serviceaccount:castai-agent:castai-cluster-controller"
return c.RequestingUser == "kubelet-bootstrap" || c.RequestingUser == serviceAccount
}

func isAlreadyApproved(err error) bool {
Expand Down Expand Up @@ -345,7 +346,7 @@ func createInformer(ctx context.Context, client kubernetes.Interface) (informers

var errUnexpectedObjectType = errors.New("unexpected object type")

func processCSREvent(ctx context.Context, c chan<- *Certificate, csrObj interface{}) error {
func processCSREvent(ctx context.Context, c chan<- *Certificate, csrObj interface{}, serviceAccount string) error {
cert, err := toCertificate(csrObj)
if err != nil {
return err
Expand All @@ -355,7 +356,7 @@ func processCSREvent(ctx context.Context, c chan<- *Certificate, csrObj interfac
return nil
}

if cert.Approved() || !cert.ForCASTAINode() || !cert.NodeBootstrap() || cert.Outdated() {
if cert.Approved() || !cert.ForCASTAINode() || !cert.NodeBootstrap(serviceAccount) || cert.Outdated() {
return nil
}

Expand All @@ -369,10 +370,12 @@ func toCertificate(obj interface{}) (cert *Certificate, err error) {

switch e := obj.(type) {
case *certv1.CertificateSigningRequest:
log.Printf("certv1.CertificateSigningRequest: %s", e.Name)
name = e.Name
request = e.Spec.Request
cert = &Certificate{Name: name, v1: e, RequestingUser: e.Spec.Username}
case *certv1beta1.CertificateSigningRequest:
log.Printf("certv1.CertificateSigningRequest: %s", e.Name)
name = e.Name
request = e.Spec.Request
cert = &Certificate{Name: name, v1Beta1: e, RequestingUser: e.Spec.Username}
Expand Down
4 changes: 2 additions & 2 deletions internal/actions/csr/csr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func Test_nodeBootstrap(t *testing.T) {
cert := &Certificate{
RequestingUser: tc.reqUser,
}
require.Equal(t, tc.want, cert.NodeBootstrap())
require.Equal(t, tc.want, cert.NodeBootstrap("system:serviceaccount:castai-agent:castai-cluster-controller"))
})
}
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func Test_toCertificate(t *testing.T) {
},
},
checkFunc: func(t *testing.T, cert *Certificate) {
require.False(t, cert.NodeBootstrap())
require.False(t, cert.NodeBootstrap("system:serviceaccount:castai-agent:castai-cluster-controller"))
},
wantErr: false,
},
Expand Down
11 changes: 7 additions & 4 deletions internal/actions/csr/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ const (
approveCSRTimeout = 4 * time.Minute
)

func NewApprovalManager(log logrus.FieldLogger, clientset kubernetes.Interface) *ApprovalManager {
func NewApprovalManager(log logrus.FieldLogger, clientset kubernetes.Interface, clusterControllerServiceAccount string) *ApprovalManager {
return &ApprovalManager{
log: log,
clientset: clientset,
log: log,
clientset: clientset,
clusterControllerServiceAccount: clusterControllerServiceAccount,
}
}

Expand All @@ -32,6 +33,8 @@ type ApprovalManager struct {
clientset kubernetes.Interface
cancelAutoApprove context.CancelFunc

clusterControllerServiceAccount string

inProgress map[string]struct{} // one handler per csr/certificate Name.
m sync.Mutex // Used to make sure there is just one watcher running.
}
Expand All @@ -46,7 +49,7 @@ func (h *ApprovalManager) Start(ctx context.Context) error {

handlerFuncs := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if err := processCSREvent(ctx, c, obj); err != nil {
if err := processCSREvent(ctx, c, obj, h.clusterControllerServiceAccount); err != nil {
h.log.WithError(err).Warn("failed to process csr add event")
}
},
Expand Down
4 changes: 2 additions & 2 deletions internal/actions/csr/svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestCSRApprove(t *testing.T) {
csrName := "node-csr-123"
userName := "kubelet-bootstrap"
client := fake.NewClientset(getCSRv1(csrName, userName))
s := NewApprovalManager(log, client)
s := NewApprovalManager(log, client, "system:serviceaccount:castai-agent:castai-cluster-controller")
watcher := watch.NewFake()
client.PrependWatchReactor("certificatesigningrequests", ktest.DefaultWatchReactor(watcher, nil))

Expand Down Expand Up @@ -111,7 +111,7 @@ func TestCSRApprove(t *testing.T) {
csrName := "123"
userName := "kubelet-bootstrap"
client := fake.NewClientset(getCSRv1(csrName, userName))
s := NewApprovalManager(log, client)
s := NewApprovalManager(log, client, "system:serviceaccount:castai-agent:castai-cluster-controller")
watcher := watch.NewFake()
client.PrependWatchReactor("certificatesigningrequests", ktest.DefaultWatchReactor(watcher, nil))

Expand Down
10 changes: 10 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/sirupsen/logrus"
Expand All @@ -28,6 +29,7 @@ type Config struct {

MonitorMetadataPath string `mapstructure:"monitor_metadata"`
SelfPod Pod `mapstructure:"self_pod"`
ServiceAccount string `mapstructure:"service_account_name"`
}

type Pod struct {
Expand Down Expand Up @@ -90,6 +92,8 @@ func Get() Config {
_ = viper.BindEnv("self_pod.node", "KUBERNETES_NODE_NAME")
_ = viper.BindEnv("self_pod.name", "KUBERNETES_POD")
_ = viper.BindEnv("self_pod.namespace", "LEADER_ELECTION_NAMESPACE")
// TODO([email protected]): update helm charts
_ = viper.BindEnv("service_account_name", "SERVICE_ACCOUNT")

cfg = &Config{}
if err := viper.Unmarshal(&cfg); err != nil {
Expand All @@ -115,6 +119,12 @@ func Get() Config {
required("LEADER_ELECTION_NAMESPACE")
}

if !strings.HasPrefix(cfg.ServiceAccount, "system:serviceaccount:") {
cfg.ServiceAccount = "system:serviceaccount:" + cfg.SelfPod.Namespace + ":" + cfg.ServiceAccount
} else if cfg.ServiceAccount == "" {
cfg.ServiceAccount = "system:serviceaccount:castai-agent:castai-cluster-controller"
}

if cfg.LeaderElection.Enabled {
if cfg.LeaderElection.LockName == "" {
required("LEADER_ELECTION_LOCK_NAME")
Expand Down
3 changes: 2 additions & 1 deletion internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func TestConfig(t *testing.T) {
SelfPod: Pod{
Namespace: "castai-agent",
},
ClusterID: "c1",
ServiceAccount: "system:serviceaccount:castai-agent:castai-cluster-controller",
ClusterID: "c1",
LeaderElection: LeaderElection{
Enabled: true,
LockName: "castai-cluster-controller",
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

// These should be set via `go build` during a release.
var (
GitCommit = "undefined"
GitCommit = "4a3f219"
GitRef = "no-ref"
Version = "local"
Version = "v0.54.6"
)

func main() {
Expand Down
Loading