-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (101 loc) · 2.62 KB
/
main.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
package main
import (
"log"
"os"
"path/filepath"
"time"
"k8s.io/client-go/tools/clientcmd"
"github.com/jonatanblue/tweet-operator/pkg/libs/k8sclient"
"github.com/jonatanblue/tweet-operator/pkg/libs/twitterclient"
"github.com/jonatanblue/tweet-operator/pkg/reconciler"
"k8s.io/client-go/rest"
tweetclient "github.com/jonatanblue/tweet-operator/pkg/client/clientset/versioned"
)
type runMode string
const (
runModeLoop = runMode("loop")
runModeRunOnce = runMode("run-once")
)
func mustLookupEnv(key string) string {
value := os.Getenv(key)
if value == "" {
log.Panicf("%s must be set", key)
}
return value
}
func inClusterConfigAvailable() bool {
host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT")
return len(host) > 0 && len(port) > 0
}
func getKubeConfig() (*rest.Config, error) {
var config *rest.Config
var err error
if inClusterConfigAvailable() {
config, err = rest.InClusterConfig()
} else {
if value, ok := os.LookupEnv("KUBECONFIG"); ok {
config, err = clientcmd.BuildConfigFromFlags("", value)
} else {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
config, err = clientcmd.BuildConfigFromFlags("", filepath.Join(homeDir, ".kube", "config"))
if err != nil {
return nil, err
}
}
}
if err != nil {
return nil, err
}
return config, nil
}
func main() {
runMode := runModeLoop
// Lookup optional run mode env var
if os.Getenv("RUN_MODE") == "run-once" {
runMode = runModeRunOnce
}
// Kubernetes client
kubeConfig, err := getKubeConfig()
if err != nil {
log.Fatal(err)
}
tweetClientSet := tweetclient.NewForConfigOrDie(kubeConfig)
tweetClient := tweetClientSet.ExampleV1().Tweets("default")
k8sClient := k8sclient.NewK8sClient(tweetClient)
// Twitter client
creds := twitterclient.Credentials{
ConsumerKey: mustLookupEnv("CONSUMER_KEY"),
ConsumerSecret: mustLookupEnv("CONSUMER_SECRET"),
AccessToken: mustLookupEnv("ACCESS_TOKEN"),
AccessTokenSecret: mustLookupEnv("ACCESS_TOKEN_SECRET"),
}
apiClient, err := twitterclient.NewTwitterAPIClient(&creds)
if err != nil {
log.Fatal(err)
}
twitterClient := twitterclient.NewTwitterClient(
apiClient.Statuses,
apiClient.Timelines,
)
// Reconciler
reconciler := reconciler.NewTweetReconciler(
k8sClient,
twitterClient,
mustLookupEnv("TWITTER_USERNAME"),
)
log.Print("Starting reconciliation loop...")
for true {
reconciled, err := reconciler.Reconcile()
if err != nil {
log.Fatal(err)
}
log.Printf("main: reconciled=%v", reconciled)
if runMode == runModeRunOnce {
break
}
<-time.After(10 * time.Second)
}
}