Skip to content
Merged
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
3 changes: 3 additions & 0 deletions internal/cli/cluster_pterodactyl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
15 changes: 15 additions & 0 deletions internal/install/liqo-provider-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 54 additions & 7 deletions internal/install/pterodactyl_reflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 13 additions & 8 deletions internal/install/pterodactyl_reflection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Loading