forked from hashicorp/terraform-provider-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (90 loc) · 3.07 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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"time"
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/terraform-exec/tfexec"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
tf5server "github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server"
tf5muxserver "github.com/hashicorp/terraform-plugin-mux/tf5muxserver"
framework "github.com/hashicorp/terraform-provider-kubernetes/internal/framework/provider"
"github.com/hashicorp/terraform-provider-kubernetes/kubernetes"
manifest "github.com/hashicorp/terraform-provider-kubernetes/manifest/provider"
)
const (
providerName = "registry.terraform.io/hashicorp/kubernetes"
Version = "dev"
)
// Generate docs for website
//go:generate go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
func main() {
debugFlag := flag.Bool("debug", false, "Start provider in stand-alone debug mode.")
flag.Parse()
providers := []func() tfprotov5.ProviderServer{
kubernetes.Provider().GRPCProvider,
manifest.Provider(),
providerserver.NewProtocol5(framework.New(Version)),
}
ctx := context.Background()
muxer, err := tf5muxserver.NewMuxServer(ctx, providers...)
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
opts := []tf5server.ServeOpt{}
if *debugFlag {
reattachConfigCh := make(chan *plugin.ReattachConfig)
go func() {
reattachConfig, err := waitForReattachConfig(reattachConfigCh)
if err != nil {
fmt.Printf("Error getting reattach config: %s\n", err)
return
}
printReattachConfig(reattachConfig)
}()
opts = append(opts, tf5server.WithDebug(ctx, reattachConfigCh, nil))
}
tf5server.Serve(providerName, muxer.ProviderServer, opts...)
}
// convertReattachConfig converts plugin.ReattachConfig to tfexec.ReattachConfig
func convertReattachConfig(reattachConfig *plugin.ReattachConfig) tfexec.ReattachConfig {
return tfexec.ReattachConfig{
Protocol: string(reattachConfig.Protocol),
Pid: reattachConfig.Pid,
Test: true,
Addr: tfexec.ReattachConfigAddr{
Network: reattachConfig.Addr.Network(),
String: reattachConfig.Addr.String(),
},
}
}
// printReattachConfig prints the line the user needs to copy and paste
// to set the TF_REATTACH_PROVIDERS variable
func printReattachConfig(config *plugin.ReattachConfig) {
reattachStr, err := json.Marshal(map[string]tfexec.ReattachConfig{
"kubernetes": convertReattachConfig(config),
})
if err != nil {
fmt.Printf("Error building reattach string: %s", err)
return
}
fmt.Printf("# Provider server started\nexport TF_REATTACH_PROVIDERS='%s'\n", string(reattachStr))
}
// waitForReattachConfig blocks until a ReattachConfig is recieved on the
// supplied channel or times out after 2 seconds.
func waitForReattachConfig(ch chan *plugin.ReattachConfig) (*plugin.ReattachConfig, error) {
select {
case config := <-ch:
return config, nil
case <-time.After(2 * time.Second):
return nil, fmt.Errorf("timeout while waiting for reattach configuration")
}
}