2024-01-22 08:01:11 -05:00
|
|
|
package scheduler_k3s
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/dokku/dokku/plugins/common"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type PortMap struct {
|
|
|
|
|
ContainerPort int32 `json:"container_port"`
|
|
|
|
|
HostPort int32 `json:"host_port"`
|
|
|
|
|
Scheme string `json:"scheme"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p PortMap) String() string {
|
|
|
|
|
return fmt.Sprintf("%s-%d-%d", p.Scheme, p.HostPort, p.ContainerPort)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p PortMap) IsAllowedHttp() bool {
|
|
|
|
|
return p.Scheme == "http" || p.ContainerPort == 80
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p PortMap) IsAllowedHttps() bool {
|
|
|
|
|
return p.Scheme == "https" || p.ContainerPort == 443
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 09:45:20 -05:00
|
|
|
func getPortMaps(appName string) (map[string]PortMap, error) {
|
2024-01-22 08:01:11 -05:00
|
|
|
portMaps := []PortMap{}
|
|
|
|
|
|
2024-01-23 09:45:20 -05:00
|
|
|
allowedMappings := map[string]PortMap{}
|
2024-03-14 00:46:55 -04:00
|
|
|
results, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
|
|
|
|
|
Trigger: "ports-get",
|
|
|
|
|
Args: []string{appName, "json"},
|
|
|
|
|
})
|
2024-01-22 08:01:11 -05:00
|
|
|
if err != nil {
|
2024-01-23 09:45:20 -05:00
|
|
|
return allowedMappings, err
|
2024-01-22 08:01:11 -05:00
|
|
|
}
|
|
|
|
|
|
2024-03-14 00:46:55 -04:00
|
|
|
err = json.Unmarshal([]byte(results.StdoutContents()), &portMaps)
|
2024-01-22 08:01:11 -05:00
|
|
|
if err != nil {
|
2024-01-23 09:45:20 -05:00
|
|
|
return allowedMappings, err
|
2024-01-22 08:01:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, portMap := range portMaps {
|
|
|
|
|
if !portMap.IsAllowedHttp() && !portMap.IsAllowedHttps() {
|
|
|
|
|
// todo: log warning
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 09:45:20 -05:00
|
|
|
allowedMappings[portMap.String()] = portMap
|
2024-01-22 08:01:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return allowedMappings, nil
|
|
|
|
|
}
|