Skip to content

Commit

Permalink
Merge pull request #772 from jlandowner/refactoring/urlbase2
Browse files Browse the repository at this point in the history
Redesign URLBase and NetworkRule
  • Loading branch information
oruharo authored Jun 29, 2023
2 parents 748d334 + ddd0b94 commit d5664bd
Show file tree
Hide file tree
Showing 83 changed files with 2,764 additions and 5,589 deletions.
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

0 comments on commit d5664bd

Please sign in to comment.