/
raw_state.go
116 lines (102 loc) · 3.14 KB
/
raw_state.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
// Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package terraformer
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
)
// Marshal transform RawState to []byte representation. It encodes the raw state data
func (trs *RawState) Marshal() ([]byte, error) {
return json.Marshal(trs.encodeBase64())
}
// GetRawState returns the content of terraform state config map
func (t *terraformer) GetRawState(ctx context.Context) (*RawState, error) {
configMap := &corev1.ConfigMap{}
if err := t.client.Get(ctx, kutil.Key(t.namespace, t.stateName), configMap); err != nil {
return nil, err
}
return &RawState{
Data: configMap.Data[StateKey],
Encoding: NoneEncoding,
}, nil
}
// UnmarshalRawState transform passed rawState to RawState struct. It tries to decode the state
func UnmarshalRawState(rawState interface{}) (*RawState, error) {
var rawData []byte
switch v := rawState.(type) {
case *runtime.RawExtension:
if v == nil {
rawData = nil
} else {
rawData = v.Raw
}
case []byte:
rawData = v
case string:
rawData = []byte(v)
case nil:
rawData = nil
default:
return nil, fmt.Errorf("unsupported type '%T' for unmarshaling Raw Terraform State", rawState)
}
terraformStateObj, err := buildRawState(rawData)
if err != nil {
return nil, err
}
return terraformStateObj.decode()
}
// buildRawState returns RawState from byte slice
func buildRawState(terraformRawState []byte) (*RawState, error) {
trs := &RawState{
Data: "",
Encoding: NoneEncoding,
}
if terraformRawState == nil {
return trs, nil
}
if err := json.Unmarshal(terraformRawState, trs); err != nil {
return nil, err
}
return trs, nil
}
// encodeBase64 encode the RawState.Data if it is not already base64 encoded
func (trs *RawState) encodeBase64() *RawState {
if trs.Encoding != Base64Encoding {
trs.Data = base64.StdEncoding.EncodeToString([]byte(trs.Data))
trs.Encoding = Base64Encoding
}
return trs
}
// decode tries to decode RawState.Data
func (trs *RawState) decode() (*RawState, error) {
switch trs.Encoding {
case Base64Encoding:
trsDec, err := base64.StdEncoding.DecodeString(trs.Data)
if err != nil {
return nil, err
}
trs.Data = string(trsDec)
trs.Encoding = NoneEncoding
case NoneEncoding:
//do nothing
default:
return nil, fmt.Errorf("unrecognised encoding %q for RawState.Data", trs.Encoding)
}
return trs, nil
}