/
btserver_helper.go
132 lines (110 loc) · 3.52 KB
/
btserver_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package controller
import (
"context"
"fmt"
"sync"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/log"
torrentv1alpha1 "github.com/alphayax/torrent-operator/api/v1alpha1"
"github.com/alphayax/torrent-operator/internal/controller/helper"
)
var (
btServers = map[string]helper.BtServerInterface{}
btServersMutex sync.Mutex
)
func getServerKey(btServer *torrentv1alpha1.BtServer) string {
return fmt.Sprintf("%s/%s", btServer.Namespace, btServer.Name)
}
func getBtServer(ctx context.Context, btServer *torrentv1alpha1.BtServer) (helper.BtServerInterface, error) {
serverKey := getServerKey(btServer)
// If server is not found, try to register it
if _, ok := btServers[serverKey]; !ok {
if err := registerServer(ctx, btServer); err != nil {
return nil, err
}
}
return btServers[serverKey], nil
}
func registerServer(ctx context.Context, k8sBtServer *torrentv1alpha1.BtServer) error {
logger := log.FromContext(ctx)
if btServers == nil {
btServers = map[string]helper.BtServerInterface{}
}
serverKey := getServerKey(k8sBtServer)
// Avoid creation of multiple clients for the same server
btServersMutex.Lock()
defer btServersMutex.Unlock()
// Check if we already have a client for this server
if _, ok := btServers[serverKey]; !ok {
logger.Info("Register server", "server", serverKey)
username, err := getUsername(ctx, k8sBtServer.Spec.Credentials)
if err != nil {
return err
}
password, err := getPassword(ctx, k8sBtServer.Spec.Credentials)
if err != nil {
return err
}
switch k8sBtServer.Spec.Type {
case "qBittorrent":
btServers[serverKey] = helper.NewQBittorrentServer(k8sBtServer.Spec)
break
case "transmission":
btServers[serverKey] = helper.NewTransmissionServer(k8sBtServer.Spec)
break
default:
return fmt.Errorf("unsupported server type: %s", k8sBtServer.Spec.Type)
}
// Login
if err := btServers[serverKey].Login(username, password); err != nil {
return err
}
logger.Info("Logged into server", "Uri", k8sBtServer.Spec.ServerUri)
}
return nil
}
func getValueFromSecret(ctx context.Context, secretName string, secretNamespace string, secretKey string) (string, error) {
cl, err := client.New(config.GetConfigOrDie(), client.Options{})
if err != nil {
return "", err
}
namespacedName := types.NamespacedName{
Name: secretName,
Namespace: secretNamespace,
}
secret := &v1.Secret{}
err = cl.Get(ctx, namespacedName, secret)
if err != nil {
return "", err
}
return string(secret.Data[secretKey]), nil
}
func getUsername(ctx context.Context, credentials torrentv1alpha1.BtServerSpecCredentials) (string, error) {
// Get username from secret if specified
if credentials.UsernameFromSecret.Name != "" {
return getValueFromSecret(
ctx,
credentials.UsernameFromSecret.Name,
credentials.UsernameFromSecret.Namespace,
credentials.UsernameFromSecret.Key,
)
}
// If not specified via a secret, return value in spec
return credentials.Username, nil
}
func getPassword(ctx context.Context, credentials torrentv1alpha1.BtServerSpecCredentials) (string, error) {
// Get password from secret if specified
if credentials.PasswordFromSecret.Name != "" {
return getValueFromSecret(
ctx,
credentials.PasswordFromSecret.Name,
credentials.PasswordFromSecret.Namespace,
credentials.PasswordFromSecret.Key,
)
}
// If not specified via a secret, return value in spec
return credentials.Password, nil
}