Skip to content

Commit 00ca65d

Browse files
committed
Add config
1 parent 028c60d commit 00ca65d

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
# slack-operator
22
Kubernetes operator for Slack
3+
4+
# TODO:
5+
- Readme for slack app/token creation
6+
- Update to operator v1.0.0
7+
- CRD tests for deletion and users invite scenario
8+
- Webhooks
9+
- Scenario: rename to existing channel, then delete the CRD what will happen?: ID will be of the channel we want to delete so no issue?

config/operator/default-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
slack:
2+
APIToken:
3+
secretName: "slack-secret"
4+
key: APIToken

config/samples/slack-secret.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: slack-secret
5+
type: Opaque
6+
data:
7+
APIToken: eG94Yi0xMjM0NTY3ODktMTIzNDU0MzIxLWFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6

main.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"context"
2021
"flag"
2122
"os"
2223

24+
v1 "k8s.io/api/core/v1"
2325
"k8s.io/apimachinery/pkg/runtime"
26+
"k8s.io/apimachinery/pkg/types"
2427
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2528
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
2629
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
2730
ctrl "sigs.k8s.io/controller-runtime"
31+
"sigs.k8s.io/controller-runtime/pkg/client"
2832
"sigs.k8s.io/controller-runtime/pkg/log/zap"
2933

3034
slackv1alpha1 "github.com/stakater/slack-operator/api/v1alpha1"
3135
"github.com/stakater/slack-operator/controllers"
36+
"github.com/stakater/slack-operator/pkg/config"
3237
slack "github.com/stakater/slack-operator/pkg/slack"
3338
// +kubebuilder:scaffold:imports
3439
)
@@ -68,11 +73,19 @@ func main() {
6873
os.Exit(1)
6974
}
7075

76+
config, err := config.GetOperatorConfig()
77+
if err != nil {
78+
setupLog.Error(err, "Unable to read operator config")
79+
os.Exit(1)
80+
}
81+
82+
slackAPIToken := readSlackTokenSecret(config, mgr.GetAPIReader())
83+
7184
if err = (&controllers.ChannelReconciler{
7285
Client: mgr.GetClient(),
7386
Log: ctrl.Log.WithName("controllers").WithName("Channel"),
7487
Scheme: mgr.GetScheme(),
75-
SlackService: slack.New("", ctrl.Log.WithName("service").WithName("Slack")), //TODO: get from config
88+
SlackService: slack.New(slackAPIToken, ctrl.Log.WithName("service").WithName("Slack")),
7689
}).SetupWithManager(mgr); err != nil {
7790
setupLog.Error(err, "unable to create controller", "controller", "Channel")
7891
os.Exit(1)
@@ -92,3 +105,26 @@ func main() {
92105
os.Exit(1)
93106
}
94107
}
108+
109+
func readSlackTokenSecret(config *config.Config, k8sReader client.Reader) string {
110+
secretName := config.Slack.APIToken.SecretName
111+
secretKey := config.Slack.APIToken.Key
112+
113+
secret := &v1.Secret{}
114+
setupLog.Info(os.Getenv("OPERATOR_NAMESPACE"))
115+
//TODO: get Operator namespace
116+
err := k8sReader.Get(context.TODO(), types.NamespacedName{Name: secretName, Namespace: "namnplats"}, secret)
117+
118+
if err != nil {
119+
setupLog.Error(err, "Could not read secret")
120+
os.Exit(1)
121+
}
122+
token := string(secret.Data[secretKey])
123+
124+
if token == "" {
125+
setupLog.Error(nil, "Could not read API token from key", "secretName", secretName, "secretKey", secretKey)
126+
os.Exit(1)
127+
}
128+
129+
return token
130+
}

pkg/config/config.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package config
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
7+
"gopkg.in/yaml.v2"
8+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
9+
)
10+
11+
// Config struct for operator config yaml
12+
type Config struct {
13+
Slack Slack `yaml:"slack"`
14+
}
15+
16+
// Slack for config yaml structure
17+
type Slack struct {
18+
APIToken APIToken `yaml:"APIToken"`
19+
}
20+
21+
// APIToken for config yaml structure
22+
type APIToken struct {
23+
SecretName string `yaml:"secretName"`
24+
Key string `yaml:"key"`
25+
}
26+
27+
var log = zap.Logger(true)
28+
29+
func readConfig(filePath string) (*Config, error) {
30+
var config Config
31+
32+
// Read YML
33+
source, err := ioutil.ReadFile(filePath)
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
// Unmarshall
39+
err = yaml.Unmarshal(source, &config)
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
return &config, nil
45+
}
46+
47+
// GetOperatorConfig returns the config object for the operator
48+
func GetOperatorConfig() (*Config, error) {
49+
configFilePath := os.Getenv("CONFIG_FILE_PATH")
50+
if len(configFilePath) == 0 {
51+
configFilePath = "config/operator/default-config.yaml"
52+
}
53+
54+
log.Info("Reading config file", "configFilePath", configFilePath)
55+
config, err := readConfig(configFilePath)
56+
if err != nil {
57+
return nil, err
58+
}
59+
return config, nil
60+
}

0 commit comments

Comments
 (0)