syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
Documentation
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! HL5xxx - Best Practice Rules
//!
//! Rules for validating Kubernetes best practices in Helm charts.

use crate::analyzer::helmlint::parser::template::TemplateToken;
use crate::analyzer::helmlint::rules::{LintContext, Rule};
use crate::analyzer::helmlint::types::{CheckFailure, RuleCategory, Severity};

/// Get all HL5xxx rules.
pub fn rules() -> Vec<Box<dyn Rule>> {
    vec![
        Box::new(HL5001),
        Box::new(HL5002),
        Box::new(HL5003),
        Box::new(HL5004),
        Box::new(HL5005),
        Box::new(HL5006),
    ]
}

/// HL5001: Missing resource limits
pub struct HL5001;

impl Rule for HL5001 {
    fn code(&self) -> &'static str {
        "HL5001"
    }

    fn severity(&self) -> Severity {
        Severity::Warning
    }

    fn name(&self) -> &'static str {
        "missing-resource-limits"
    }

    fn description(&self) -> &'static str {
        "Container should have resource limits defined"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        let mut failures = Vec::new();

        // Check values.yaml for resource limits
        if let Some(values) = ctx.values {
            let has_limits = values
                .defined_paths
                .iter()
                .any(|p| p.contains("resources.limits") || p.ends_with(".limits"));

            if !has_limits {
                failures.push(CheckFailure::new(
                    "HL5001",
                    Severity::Warning,
                    "No resource limits found in values.yaml. Define resources.limits for predictable resource usage",
                    "values.yaml",
                    1,
                    RuleCategory::BestPractice,
                ));
            }
        }

        failures
    }
}

/// HL5002: Missing resource requests
pub struct HL5002;

impl Rule for HL5002 {
    fn code(&self) -> &'static str {
        "HL5002"
    }

    fn severity(&self) -> Severity {
        Severity::Warning
    }

    fn name(&self) -> &'static str {
        "missing-resource-requests"
    }

    fn description(&self) -> &'static str {
        "Container should have resource requests defined"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        let mut failures = Vec::new();

        if let Some(values) = ctx.values {
            let has_requests = values
                .defined_paths
                .iter()
                .any(|p| p.contains("resources.requests") || p.ends_with(".requests"));

            if !has_requests {
                failures.push(CheckFailure::new(
                    "HL5002",
                    Severity::Warning,
                    "No resource requests found in values.yaml. Define resources.requests for proper scheduling",
                    "values.yaml",
                    1,
                    RuleCategory::BestPractice,
                ));
            }
        }

        failures
    }
}

/// HL5003: Missing liveness probe
pub struct HL5003;

impl Rule for HL5003 {
    fn code(&self) -> &'static str {
        "HL5003"
    }

    fn severity(&self) -> Severity {
        Severity::Info
    }

    fn name(&self) -> &'static str {
        "missing-liveness-probe"
    }

    fn description(&self) -> &'static str {
        "Container should have a liveness probe for health checking"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        let mut failures = Vec::new();

        // Check if any template has livenessProbe
        let has_liveness_in_template = ctx.templates.iter().any(|t| {
            t.tokens.iter().any(|token| match token {
                TemplateToken::Text { content, .. } => content.contains("livenessProbe"),
                TemplateToken::Action { content, .. } => content.contains("livenessProbe"),
                _ => false,
            })
        });

        // Check values.yaml
        let has_liveness_in_values = ctx
            .values
            .map(|v| {
                v.defined_paths
                    .iter()
                    .any(|p| p.to_lowercase().contains("livenessprobe"))
            })
            .unwrap_or(false);

        if !has_liveness_in_template && !has_liveness_in_values {
            failures.push(CheckFailure::new(
                "HL5003",
                Severity::Info,
                "No livenessProbe found. Consider adding a liveness probe for container health monitoring",
                "templates/",
                1,
                RuleCategory::BestPractice,
            ));
        }

        failures
    }
}

/// HL5004: Missing readiness probe
pub struct HL5004;

impl Rule for HL5004 {
    fn code(&self) -> &'static str {
        "HL5004"
    }

    fn severity(&self) -> Severity {
        Severity::Info
    }

    fn name(&self) -> &'static str {
        "missing-readiness-probe"
    }

    fn description(&self) -> &'static str {
        "Container should have a readiness probe for traffic management"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        let mut failures = Vec::new();

        let has_readiness_in_template = ctx.templates.iter().any(|t| {
            t.tokens.iter().any(|token| match token {
                TemplateToken::Text { content, .. } => content.contains("readinessProbe"),
                TemplateToken::Action { content, .. } => content.contains("readinessProbe"),
                _ => false,
            })
        });

        let has_readiness_in_values = ctx
            .values
            .map(|v| {
                v.defined_paths
                    .iter()
                    .any(|p| p.to_lowercase().contains("readinessprobe"))
            })
            .unwrap_or(false);

        if !has_readiness_in_template && !has_readiness_in_values {
            failures.push(CheckFailure::new(
                "HL5004",
                Severity::Info,
                "No readinessProbe found. Consider adding a readiness probe for proper load balancing",
                "templates/",
                1,
                RuleCategory::BestPractice,
            ));
        }

        failures
    }
}

/// HL5005: Using deprecated Kubernetes API
pub struct HL5005;

impl Rule for HL5005 {
    fn code(&self) -> &'static str {
        "HL5005"
    }

    fn severity(&self) -> Severity {
        Severity::Error
    }

    fn name(&self) -> &'static str {
        "deprecated-api"
    }

    fn description(&self) -> &'static str {
        "Template uses deprecated Kubernetes API version"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        let mut failures = Vec::new();

        // Deprecated APIs and their replacements
        let deprecated_apis = [
            (
                "extensions/v1beta1",
                "apps/v1",
                "Deployment, DaemonSet, ReplicaSet",
            ),
            ("apps/v1beta1", "apps/v1", "Deployment, StatefulSet"),
            (
                "apps/v1beta2",
                "apps/v1",
                "Deployment, StatefulSet, DaemonSet, ReplicaSet",
            ),
            (
                "networking.k8s.io/v1beta1",
                "networking.k8s.io/v1",
                "Ingress",
            ),
            (
                "rbac.authorization.k8s.io/v1beta1",
                "rbac.authorization.k8s.io/v1",
                "Role, ClusterRole, RoleBinding",
            ),
            (
                "admissionregistration.k8s.io/v1beta1",
                "admissionregistration.k8s.io/v1",
                "MutatingWebhookConfiguration, ValidatingWebhookConfiguration",
            ),
            (
                "apiextensions.k8s.io/v1beta1",
                "apiextensions.k8s.io/v1",
                "CustomResourceDefinition",
            ),
            ("policy/v1beta1", "policy/v1", "PodDisruptionBudget"),
            ("batch/v1beta1", "batch/v1", "CronJob"),
        ];

        for template in ctx.templates {
            for token in &template.tokens {
                if let TemplateToken::Text { content, line } = token {
                    for (deprecated, replacement, resources) in &deprecated_apis {
                        if content.contains(&format!("apiVersion: {}", deprecated)) {
                            failures.push(CheckFailure::new(
                                "HL5005",
                                Severity::Error,
                                format!(
                                    "Deprecated API '{}' for {}. Use '{}' instead",
                                    deprecated, resources, replacement
                                ),
                                &template.path,
                                *line,
                                RuleCategory::BestPractice,
                            ));
                        }
                    }
                }
            }
        }

        failures
    }
}

/// HL5006: Labels missing recommended keys
pub struct HL5006;

impl Rule for HL5006 {
    fn code(&self) -> &'static str {
        "HL5006"
    }

    fn severity(&self) -> Severity {
        Severity::Info
    }

    fn name(&self) -> &'static str {
        "missing-recommended-labels"
    }

    fn description(&self) -> &'static str {
        "Resources should have recommended Kubernetes labels"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        let mut failures = Vec::new();

        // Recommended labels per Kubernetes best practices
        let recommended_labels = [
            "app.kubernetes.io/name",
            "app.kubernetes.io/instance",
            "app.kubernetes.io/version",
            "app.kubernetes.io/component",
            "app.kubernetes.io/part-of",
            "app.kubernetes.io/managed-by",
        ];

        // Check if helpers define standard labels
        let has_labels_helper = ctx
            .helpers
            .map(|h| {
                h.helpers.iter().any(|helper| {
                    helper.name.contains("labels") || helper.name.contains("selectorLabels")
                })
            })
            .unwrap_or(false);

        if !has_labels_helper {
            // Check templates for any recommended labels
            let has_recommended_labels = ctx.templates.iter().any(|t| {
                t.tokens.iter().any(|token| match token {
                    TemplateToken::Text { content, .. } => {
                        recommended_labels.iter().any(|l| content.contains(l))
                    }
                    _ => false,
                })
            });

            if !has_recommended_labels {
                failures.push(CheckFailure::new(
                    "HL5006",
                    Severity::Info,
                    "No recommended Kubernetes labels found. Consider adding app.kubernetes.io/* labels",
                    "templates/_helpers.tpl",
                    1,
                    RuleCategory::BestPractice,
                ));
            }
        }

        failures
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rules_exist() {
        let all_rules = rules();
        assert!(!all_rules.is_empty());
    }

    #[test]
    fn test_deprecated_api_list() {
        // Verify our deprecated API list is reasonable
        let deprecated_apis = [
            "extensions/v1beta1",
            "apps/v1beta1",
            "apps/v1beta2",
            "networking.k8s.io/v1beta1",
        ];

        for api in &deprecated_apis {
            assert!(api.contains("beta") || api.contains("v1beta"));
        }
    }
}