|
| 1 | +/* |
| 2 | +Provides a resource to create a monitor tmp_tke_basic_config |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_monitor_tmp_tke_basic_config" "tmp_tke_basic_config" { |
| 8 | + instance_id = "prom-xxxxxx" |
| 9 | + cluster_type = "eks" |
| 10 | + cluster_id = "cls-xxxxxx" |
| 11 | + name = "kube-system/kube-state-metrics" |
| 12 | + metrics_name = ["kube_job_status_succeeded"] |
| 13 | +} |
| 14 | +
|
| 15 | +``` |
| 16 | +*/ |
| 17 | +package tencentcloud |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "log" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 26 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 27 | + monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + SERVICE_MONITORS string = "service_monitors" |
| 32 | + POD_MONITORS string = "pod_monitors" |
| 33 | + RAW_JOBS string = "raw_jobs" |
| 34 | +) |
| 35 | + |
| 36 | +func resourceTencentCloudMonitorTmpTkeBasicConfig() *schema.Resource { |
| 37 | + return &schema.Resource{ |
| 38 | + Create: resourceTencentCloudMonitorTmpTkeBasicConfigCreate, |
| 39 | + Read: resourceTencentCloudMonitorTmpTkeBasicConfigRead, |
| 40 | + Update: resourceTencentCloudMonitorTmpTkeBasicConfigUpdate, |
| 41 | + Delete: resourceTencentCloudMonitorTmpTkeBasicConfigDelete, |
| 42 | + |
| 43 | + Schema: map[string]*schema.Schema{ |
| 44 | + "instance_id": { |
| 45 | + Required: true, |
| 46 | + Type: schema.TypeString, |
| 47 | + Description: "ID of instance.", |
| 48 | + }, |
| 49 | + |
| 50 | + "cluster_type": { |
| 51 | + Required: true, |
| 52 | + Type: schema.TypeString, |
| 53 | + Description: "Type of cluster.", |
| 54 | + }, |
| 55 | + |
| 56 | + "cluster_id": { |
| 57 | + Required: true, |
| 58 | + Type: schema.TypeString, |
| 59 | + Description: "ID of cluster.", |
| 60 | + }, |
| 61 | + |
| 62 | + "name": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Required: true, |
| 65 | + Description: "Name. The naming rule is: namespace/name. If you don't have any namespace, use the default namespace: kube-system, otherwise use the specified one.", |
| 66 | + }, |
| 67 | + |
| 68 | + "metrics_name": { |
| 69 | + Type: schema.TypeSet, |
| 70 | + Required: true, |
| 71 | + Elem: &schema.Schema{ |
| 72 | + Type: schema.TypeString, |
| 73 | + ValidateFunc: validateNotEmpty, |
| 74 | + }, |
| 75 | + Description: "Configure the name of the metric to keep on.", |
| 76 | + }, |
| 77 | + |
| 78 | + "config_type": { |
| 79 | + Computed: true, |
| 80 | + Type: schema.TypeString, |
| 81 | + Description: "config type, `service_monitors`, `pod_monitors`, `raw_jobs`.", |
| 82 | + }, |
| 83 | + |
| 84 | + "config": { |
| 85 | + Computed: true, |
| 86 | + Type: schema.TypeString, |
| 87 | + Description: "Full configuration in yaml format.", |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func resourceTencentCloudMonitorTmpTkeBasicConfigCreate(d *schema.ResourceData, meta interface{}) error { |
| 94 | + defer logElapsed("resource.tencentcloud_monitor_tmp_tke_basic_config.create")() |
| 95 | + defer inconsistentCheck(d, meta)() |
| 96 | + |
| 97 | + var ( |
| 98 | + instanceId string |
| 99 | + clusterType string |
| 100 | + clusterId string |
| 101 | + name string |
| 102 | + ) |
| 103 | + if v, ok := d.GetOk("instance_id"); ok { |
| 104 | + instanceId = v.(string) |
| 105 | + } |
| 106 | + if v, ok := d.GetOk("cluster_id"); ok { |
| 107 | + clusterId = v.(string) |
| 108 | + } |
| 109 | + if v, ok := d.GetOk("cluster_type"); ok { |
| 110 | + clusterType = v.(string) |
| 111 | + } |
| 112 | + if v, ok := d.GetOk("name"); ok { |
| 113 | + name = v.(string) |
| 114 | + } |
| 115 | + |
| 116 | + d.SetId(strings.Join([]string{instanceId, clusterType, clusterId, name}, FILED_SP)) |
| 117 | + |
| 118 | + return resourceTencentCloudMonitorTmpTkeBasicConfigUpdate(d, meta) |
| 119 | +} |
| 120 | + |
| 121 | +func resourceTencentCloudMonitorTmpTkeBasicConfigRead(d *schema.ResourceData, meta interface{}) error { |
| 122 | + defer logElapsed("resource.tencentcloud_monitor_tmp_tke_basic_config.read")() |
| 123 | + defer inconsistentCheck(d, meta)() |
| 124 | + |
| 125 | + logId := getLogId(contextNil) |
| 126 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 127 | + |
| 128 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 129 | + if len(idSplit) != 4 { |
| 130 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 131 | + } |
| 132 | + instanceId := idSplit[0] |
| 133 | + clusterType := idSplit[1] |
| 134 | + clusterId := idSplit[2] |
| 135 | + name := idSplit[3] |
| 136 | + |
| 137 | + service := MonitorService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 138 | + tmpTkeBasicConfig, err := service.DescribeTkeTmpBasicConfigById(ctx, clusterId, clusterType, instanceId) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + if tmpTkeBasicConfig == nil { |
| 144 | + d.SetId("") |
| 145 | + log.Printf("[WARN]%s resource `MonitorTmpTkeBasicConfig` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 146 | + return nil |
| 147 | + } |
| 148 | + |
| 149 | + _ = d.Set("instance_id", instanceId) |
| 150 | + _ = d.Set("cluster_type", clusterType) |
| 151 | + _ = d.Set("cluster_id", clusterId) |
| 152 | + _ = d.Set("name", name) |
| 153 | + |
| 154 | + configType, config, err := service.GetConfigType(name, tmpTkeBasicConfig) |
| 155 | + if err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + _ = d.Set("config_type", configType) |
| 159 | + _ = d.Set("config", config.Config) |
| 160 | + |
| 161 | + return nil |
| 162 | +} |
| 163 | + |
| 164 | +func resourceTencentCloudMonitorTmpTkeBasicConfigUpdate(d *schema.ResourceData, meta interface{}) error { |
| 165 | + defer logElapsed("resource.tencentcloud_monitor_tmp_tke_basic_config.update")() |
| 166 | + defer inconsistentCheck(d, meta)() |
| 167 | + |
| 168 | + logId := getLogId(contextNil) |
| 169 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 170 | + |
| 171 | + request := monitor.NewModifyPrometheusConfigRequest() |
| 172 | + |
| 173 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 174 | + if len(idSplit) != 4 { |
| 175 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 176 | + } |
| 177 | + instanceId := idSplit[0] |
| 178 | + clusterType := idSplit[1] |
| 179 | + clusterId := idSplit[2] |
| 180 | + name := idSplit[3] |
| 181 | + |
| 182 | + request.InstanceId = &instanceId |
| 183 | + request.ClusterType = &clusterType |
| 184 | + request.ClusterId = &clusterId |
| 185 | + |
| 186 | + if v, ok := d.GetOk("metrics_name"); ok { |
| 187 | + regexs := []string{} |
| 188 | + regexSet := v.(*schema.Set).List() |
| 189 | + for i := range regexSet { |
| 190 | + regex := regexSet[i].(string) |
| 191 | + regexs = append(regexs, regex) |
| 192 | + } |
| 193 | + |
| 194 | + service := MonitorService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 195 | + tmpTkeBasicConfig, err := service.DescribeTkeTmpBasicConfigById(ctx, clusterId, clusterType, instanceId) |
| 196 | + if err != nil { |
| 197 | + return err |
| 198 | + } |
| 199 | + configType, config, err := service.GetConfigType(name, tmpTkeBasicConfig) |
| 200 | + if err != nil { |
| 201 | + return err |
| 202 | + } |
| 203 | + |
| 204 | + serviceMonitors, podMonitors, rawMobs, err := configInit(configType, config, regexs) |
| 205 | + if err != nil { |
| 206 | + return err |
| 207 | + } |
| 208 | + |
| 209 | + if serviceMonitors != "" { |
| 210 | + prometheusConfig := []*monitor.PrometheusConfigItem{} |
| 211 | + prometheusConfig = append(prometheusConfig, &monitor.PrometheusConfigItem{ |
| 212 | + Name: &name, |
| 213 | + Config: &serviceMonitors, |
| 214 | + }) |
| 215 | + request.ServiceMonitors = prometheusConfig |
| 216 | + } |
| 217 | + if podMonitors != "" { |
| 218 | + prometheusConfig := []*monitor.PrometheusConfigItem{} |
| 219 | + prometheusConfig = append(prometheusConfig, &monitor.PrometheusConfigItem{ |
| 220 | + Name: &name, |
| 221 | + Config: &podMonitors, |
| 222 | + }) |
| 223 | + request.PodMonitors = prometheusConfig |
| 224 | + } |
| 225 | + if rawMobs != "" { |
| 226 | + prometheusConfig := []*monitor.PrometheusConfigItem{} |
| 227 | + prometheusConfig = append(prometheusConfig, &monitor.PrometheusConfigItem{ |
| 228 | + Name: &name, |
| 229 | + Config: &rawMobs, |
| 230 | + }) |
| 231 | + request.RawJobs = prometheusConfig |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 236 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseMonitorClient().ModifyPrometheusConfig(request) |
| 237 | + if e != nil { |
| 238 | + return retryError(e) |
| 239 | + } else { |
| 240 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 241 | + } |
| 242 | + return nil |
| 243 | + }) |
| 244 | + if err != nil { |
| 245 | + log.Printf("[CRITAL]%s update monitor tmpTkeBasicConfig failed, reason:%+v", logId, err) |
| 246 | + return err |
| 247 | + } |
| 248 | + |
| 249 | + return resourceTencentCloudMonitorTmpTkeBasicConfigRead(d, meta) |
| 250 | +} |
| 251 | + |
| 252 | +func resourceTencentCloudMonitorTmpTkeBasicConfigDelete(d *schema.ResourceData, meta interface{}) error { |
| 253 | + defer logElapsed("resource.tencentcloud_monitor_tmp_tke_basic_config.delete")() |
| 254 | + defer inconsistentCheck(d, meta)() |
| 255 | + |
| 256 | + return nil |
| 257 | +} |
| 258 | + |
| 259 | +func configInit(configType string, respParams *monitor.PrometheusConfigItem, regexs []string) (serviceMonitorConfig, podMonitorConfig, rawMobConfig string, errRet error) { |
| 260 | + config := PrometheusConfig{ |
| 261 | + Config: respParams.Config, |
| 262 | + Regex: regexs, |
| 263 | + } |
| 264 | + switch configType { |
| 265 | + case SERVICE_MONITORS: |
| 266 | + serviceMonitor, err := config.UnmarshalToMap() |
| 267 | + if err != nil { |
| 268 | + errRet = err |
| 269 | + return |
| 270 | + } |
| 271 | + spec := serviceMonitor["spec"].(map[interface{}]interface{})["endpoints"].([]interface{}) |
| 272 | + serviceMonitors, err := config.SetRegex(spec) |
| 273 | + serviceMonitor["spec"].(map[interface{}]interface{})["endpoints"] = serviceMonitors |
| 274 | + if err != nil { |
| 275 | + errRet = err |
| 276 | + return |
| 277 | + } |
| 278 | + serviceMonitorConfig, errRet = config.MarshalToYaml(&serviceMonitor) |
| 279 | + return |
| 280 | + case POD_MONITORS: |
| 281 | + serviceMonitor, err := config.UnmarshalToMap() |
| 282 | + if err != nil { |
| 283 | + errRet = err |
| 284 | + return |
| 285 | + } |
| 286 | + spec := serviceMonitor["spec"].(map[interface{}]interface{})["podMetricsEndpoints"].([]interface{}) |
| 287 | + serviceMonitors, err := config.SetRegex(spec) |
| 288 | + serviceMonitor["spec"].(map[interface{}]interface{})["podMetricsEndpoints"] = serviceMonitors |
| 289 | + if err != nil { |
| 290 | + errRet = err |
| 291 | + return |
| 292 | + } |
| 293 | + podMonitorConfig, errRet = config.MarshalToYaml(&serviceMonitor) |
| 294 | + return |
| 295 | + case RAW_JOBS: |
| 296 | + rawMob, err := config.UnmarshalToMap() |
| 297 | + if err != nil { |
| 298 | + errRet = err |
| 299 | + return |
| 300 | + } |
| 301 | + configs := rawMob["scrape_configs"].([]interface{}) |
| 302 | + rawMobConfigs, err := config.SetRegex(configs) |
| 303 | + rawMob["scrape_configs"] = rawMobConfigs |
| 304 | + if err != nil { |
| 305 | + errRet = err |
| 306 | + return |
| 307 | + } |
| 308 | + rawMobConfig, errRet = config.MarshalToYaml(&rawMob) |
| 309 | + return |
| 310 | + } |
| 311 | + return |
| 312 | +} |
0 commit comments