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

Redesign URLBase and NetworkRule #772

Merged
merged 7 commits into from
Jun 29, 2023
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
104 changes: 0 additions & 104 deletions api/v1alpha1/__snapshots__/workspace_types_test.snap

This file was deleted.

92 changes: 65 additions & 27 deletions api/v1alpha1/workspace_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package v1alpha1

import (
"fmt"
"net/url"
"strings"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"
)

func init() {
Expand Down Expand Up @@ -58,12 +59,10 @@ type Config struct {
DeploymentName string `json:"deploymentName,omitempty"`
ServiceName string `json:"serviceName,omitempty"`
ServiceMainPortName string `json:"mainServicePortName,omitempty"`
URLBase string `json:"urlbase,omitempty"`
}

const (
// WorkspaceTemplateAnnKeys are annotation keys for WorkspaceConfig
WorkspaceTemplateAnnKeyURLBase = "workspace.cosmo-workspace.github.io/urlbase"
WorkspaceTemplateAnnKeyDeploymentName = "workspace.cosmo-workspace.github.io/deployment"
WorkspaceTemplateAnnKeyServiceName = "workspace.cosmo-workspace.github.io/service"
WorkspaceTemplateAnnKeyServiceMainPort = "workspace.cosmo-workspace.github.io/service-main-port"
Expand All @@ -78,28 +77,49 @@ const (

// NetworkRule is an abstract network configuration rule for workspace
type NetworkRule struct {
Name string `json:"name"`
PortNumber int32 `json:"portNumber"`
HTTPPath string `json:"httpPath"`
TargetPortNumber *int32 `json:"targetPortNumber,omitempty"`
Host *string `json:"host,omitempty"`
Group *string `json:"group,omitempty"`
Public bool `json:"public"`
Protocol string `json:"protocol"`
PortNumber int32 `json:"portNumber"`
CustomHostPrefix string `json:"customHostPrefix,omitempty"`
HTTPPath string `json:"httpPath,omitempty"`
TargetPortNumber *int32 `json:"targetPortNumber,omitempty"`
Public bool `json:"public"`
}

func (r *NetworkRule) Default() {
if r.HTTPPath == "" {
r.HTTPPath = "/"
func HTTPUniqueKey(host, httpPath string) string {
return fmt.Sprintf("http://%s%s", host, httpPath)
}

func (r *NetworkRule) UniqueKey() string {
if r.Protocol == "http" {
return HTTPUniqueKey(r.HostPrefix(), r.HTTPPath)
}
if r.Group == nil || *r.Group == "" {
r.Group = &r.Name
return r.HostPrefix()
}

func MainRuleKey(cfg Config) string {
return HTTPUniqueKey(cfg.ServiceMainPortName, "/")
}

func (r *NetworkRule) HostPrefix() string {
if r.CustomHostPrefix != "" {
return r.CustomHostPrefix
}
return r.portName()
}

func (r *NetworkRule) portName() string {
return fmt.Sprintf("port%d", r.PortNumber)
}

func (r *NetworkRule) Default() {
if r.HTTPPath == "" {
r.HTTPPath = "/"
}
if r.Protocol == "" {
r.Protocol = "http"
}
}

func (r *NetworkRule) ServicePort() corev1.ServicePort {
targetPort := r.PortNumber
if r.TargetPortNumber != nil && *r.TargetPortNumber != 0 {
Expand All @@ -114,18 +134,36 @@ func (r *NetworkRule) ServicePort() corev1.ServicePort {
}
}

func NetworkRulesByService(svc corev1.Service) []NetworkRule {
netRules := make([]NetworkRule, 0, len(svc.Spec.Ports))
for _, p := range svc.Spec.Ports {
var netRule NetworkRule
netRule.Name = p.Name
netRule.PortNumber = p.Port
const (
DefaultHostBase = "{{NETRULE}}-{{WORKSPACE}}-{{USER}}"
URLVarNetRule = "{{NETRULE}}"
URLVarWorkspaceName = "{{WORKSPACE}}"
URLVarUserName = "{{USER}}"
)

func GenHost(hostbase, domain, hostprefix string, ws Workspace) string {
host := hostbase
if host == "" {
host = DefaultHostBase
}
host = strings.ReplaceAll(host, URLVarNetRule, hostprefix)
host = strings.ReplaceAll(host, URLVarWorkspaceName, ws.GetName())
userName := UserNameByNamespace(ws.GetNamespace())
if userName == "" {
userName = "default"
}
host = strings.ReplaceAll(host, URLVarUserName, userName)
if domain == "" {
return host
}
return fmt.Sprintf("%s.%s", host, domain)
}

if p.TargetPort.IntValue() != 0 {
netRule.TargetPortNumber = pointer.Int32(int32(p.TargetPort.IntValue()))
}
netRule.Default()
netRules = append(netRules, netRule)
func GenURL(protocol, host, path string) string {
u := url.URL{
Scheme: protocol,
Host: host,
Path: path,
}
return netRules
return u.String()
}
Loading