diff --git a/internal/cli/cluster_pterodactyl.go b/internal/cli/cluster_pterodactyl.go index 2741aa8..d1fa400 100644 --- a/internal/cli/cluster_pterodactyl.go +++ b/internal/cli/cluster_pterodactyl.go @@ -260,6 +260,9 @@ func applyPterodactylRelease(cmd *cobra.Command, install pterodactylInstall) err if err := helm.Run(); err != nil { return fmt.Errorf("install pterodactyl operator: %w", err) } + if err := nrinstall.EnsurePterodactylLiqoRBAC(ctx, kubeconfig); err != nil { + return fmt.Errorf("update Liqo reflection RBAC: %w", err) + } fmt.Fprintln(cmd.OutOrStdout(), "panel keys are stored only in the agent Secret") return nil } diff --git a/internal/install/liqo-provider-values.yaml b/internal/install/liqo-provider-values.yaml index a602ee8..d10a1a6 100644 --- a/internal/install/liqo-provider-values.yaml +++ b/internal/install/liqo-provider-values.yaml @@ -151,6 +151,21 @@ offloading: resource: pterodactylgamecatalogs workers: 2 type: AllowList + - group: gs.pterodactyl.com + version: v1alpha1 + resource: pterodactylcontrols + workers: 2 + type: AllowList + - group: gs.pterodactyl.com + version: v1alpha1 + resource: pterodactylnodes + workers: 2 + type: AllowList + - group: gs.pterodactyl.com + version: v1alpha1 + resource: pterodactylwebsocketrequests + workers: 2 + type: AllowList skip: annotations: - kopf.zalando.org/last-handled-configuration diff --git a/internal/install/pterodactyl_reflection.go b/internal/install/pterodactyl_reflection.go index 9761322..af48699 100644 --- a/internal/install/pterodactyl_reflection.go +++ b/internal/install/pterodactyl_reflection.go @@ -11,17 +11,30 @@ import ( "k8s.io/client-go/kubernetes" "github.com/noderings/cli/internal/config" + "github.com/noderings/cli/internal/k8s" ) const ( catalogAPIGroup = config.PterodactylCRDAPIGroup - catalogResource = "pterodactylgamecatalogs" vkRoleLocal = "liqo-virtual-kubelet-local" vkRoleRemote = "liqo-virtual-kubelet-remote" ) +// pterodactylReflectedResources is every gs.pterodactyl.com type the virtual +// kubelet is told to reflect. A namespace reflector does not become ready +// until each of those lists succeeds, so a missing rule stalls the game +// catalog the same way it would stall a VirtFusion node. +var pterodactylReflectedResources = []string{ + "pterodactylservers", + "pterodactylactions", + "pterodactylgamecatalogs", + "pterodactylcontrols", + "pterodactylnodes", + "pterodactylwebsocketrequests", +} + // PrepareGameCatalogReflection installs the operator CRDs and the Liqo role -// rules a new virtual-kubelet needs to reflect PterodactylGameCatalog. +// rules a new virtual-kubelet needs to reflect Pterodactyl resources. // Already-installed Liqo does not re-apply its values, so this runs on every // install, including the skip path. func (l *LiqoManager) PrepareGameCatalogReflection(ctx context.Context) error { @@ -34,6 +47,30 @@ func (l *LiqoManager) PrepareGameCatalogReflection(ctx context.Context) error { return ensureCatalogClusterRoles(ctx, l.k8sClient.GetClientset()) } +// EnsurePterodactylLiqoRBAC merges the Pterodactyl reflection rules into the +// Liqo virtual-kubelet roles. A Proxmox agent that later runs install-pterodactyl +// already has Liqo, and that command does not re-run liqoctl, so this is what +// updates the roles. When Liqo is not installed yet the roles are absent and +// the later register path adds them. +func EnsurePterodactylLiqoRBAC(ctx context.Context, kubeconfig string) error { + client, err := k8s.NewClient(kubeconfig) + if err != nil { + return fmt.Errorf("kubernetes client: %w", err) + } + cs := client.GetClientset() + if cs == nil { + return fmt.Errorf("kubernetes client is not connected") + } + _, err = cs.RbacV1().ClusterRoles().Get(ctx, vkRoleLocal, metav1.GetOptions{}) + if apierrors.IsNotFound(err) { + return nil + } + if err != nil { + return fmt.Errorf("get ClusterRole %s: %w", vkRoleLocal, err) + } + return ensureCatalogClusterRoles(ctx, cs) +} + func ensureCatalogClusterRoles(ctx context.Context, cs kubernetes.Interface) error { if err := ensureCatalogRole(ctx, cs, vkRoleLocal, localCatalogRules()); err != nil { return err @@ -61,16 +98,26 @@ func ensureCatalogRole(ctx context.Context, cs kubernetes.Interface, name string } func localCatalogRules() []rbacv1.PolicyRule { - return []rbacv1.PolicyRule{ - {APIGroups: []string{catalogAPIGroup}, Resources: []string{catalogResource}, Verbs: []string{"get", "list", "watch"}}, - {APIGroups: []string{catalogAPIGroup}, Resources: []string{catalogResource + "/status"}, Verbs: []string{"get", "patch", "update"}}, + rules := make([]rbacv1.PolicyRule, 0, len(pterodactylReflectedResources)*2) + for _, resource := range pterodactylReflectedResources { + rules = append(rules, + rbacv1.PolicyRule{APIGroups: []string{catalogAPIGroup}, Resources: []string{resource}, Verbs: []string{"get", "list", "watch"}}, + rbacv1.PolicyRule{APIGroups: []string{catalogAPIGroup}, Resources: []string{resource + "/status"}, Verbs: []string{"get", "patch", "update"}}, + ) } + return rules } func remoteCatalogRules() []rbacv1.PolicyRule { - return []rbacv1.PolicyRule{ - {APIGroups: []string{catalogAPIGroup}, Resources: []string{catalogResource}, Verbs: []string{"create", "delete", "get", "list", "patch", "update", "watch"}}, + rules := make([]rbacv1.PolicyRule, 0, len(pterodactylReflectedResources)) + for _, resource := range pterodactylReflectedResources { + rules = append(rules, rbacv1.PolicyRule{ + APIGroups: []string{catalogAPIGroup}, + Resources: []string{resource}, + Verbs: []string{"create", "delete", "get", "list", "patch", "update", "watch"}, + }) } + return rules } func mergeCatalogRules(have, want []rbacv1.PolicyRule) ([]rbacv1.PolicyRule, bool) { diff --git a/internal/install/pterodactyl_reflection_test.go b/internal/install/pterodactyl_reflection_test.go index 06d54e5..f3064e0 100644 --- a/internal/install/pterodactyl_reflection_test.go +++ b/internal/install/pterodactyl_reflection_test.go @@ -7,17 +7,22 @@ import ( ) func TestMergeCatalogRulesSkipsCoveredVerbs(t *testing.T) { - have := []rbacv1.PolicyRule{{ - APIGroups: []string{catalogAPIGroup}, - Resources: []string{catalogResource}, - Verbs: []string{"get", "list", "watch"}, - }} - next, changed := mergeCatalogRules(have, localCatalogRules()[:1]) + local := localCatalogRules() + have := []rbacv1.PolicyRule{local[0]} + next, changed := mergeCatalogRules(have, []rbacv1.PolicyRule{local[0]}) if changed || len(next) != 1 { t.Fatalf("covered rule was added: changed=%v len=%d", changed, len(next)) } - next, changed = mergeCatalogRules(have, remoteCatalogRules()) - if !changed || len(next) != 2 { + remote := remoteCatalogRules() + next, changed = mergeCatalogRules(have, remote) + if !changed || len(next) != 1+len(remote) { t.Fatalf("missing verbs were not added: changed=%v len=%d", changed, len(next)) } + if !catalogRuleCovered(next, rbacv1.PolicyRule{ + APIGroups: []string{catalogAPIGroup}, + Resources: []string{"pterodactylnodes"}, + Verbs: []string{"get", "list", "watch"}, + }) { + t.Fatal("node list was not granted") + } }