Nếu điều kiện trong tệp tpl

Cấu trúc điều khiển (được gọi là "hành động" theo cách nói của mẫu) cung cấp cho bạn, tác giả mẫu, khả năng kiểm soát luồng tạo mẫu. Ngôn ngữ mẫu của Helm cung cấp các cấu trúc điều khiển sau

  • # Source: mychart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eyewitness-elk-configmap
    data:
      myvalue: "Hello World"
      drink: "coffee"
      food: "PIZZA"
      mug: "true"
    
    3/
    # Source: mychart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eyewitness-elk-configmap
    data:
      myvalue: "Hello World"
      drink: "coffee"
      food: "PIZZA"
      mug: "true"
    
    4 để tạo các khối có điều kiện
  • # Source: mychart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eyewitness-elk-configmap
    data:
      myvalue: "Hello World"
      drink: "coffee"
      food: "PIZZA"
      mug: "true"
    
    5 để chỉ định phạm vi
  • # Source: mychart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eyewitness-elk-configmap
    data:
      myvalue: "Hello World"
      drink: "coffee"
      food: "PIZZA"
      mug: "true"
    
    6, cung cấp vòng lặp kiểu "cho mỗi"

Ngoài những điều này, nó cung cấp một số hành động để khai báo và sử dụng các phân đoạn mẫu được đặt tên

  • # Source: mychart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eyewitness-elk-configmap
    data:
      myvalue: "Hello World"
      drink: "coffee"
      food: "PIZZA"
      mug: "true"
    
    7 tuyên bố một mẫu có tên mới bên trong mẫu của bạn
  • # Source: mychart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eyewitness-elk-configmap
    data:
      myvalue: "Hello World"
      drink: "coffee"
      food: "PIZZA"
      mug: "true"
    
    8 nhập một mẫu có tên
  • # Source: mychart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eyewitness-elk-configmap
    data:
      myvalue: "Hello World"
      drink: "coffee"
      food: "PIZZA"
      mug: "true"
    
    9 khai báo một loại khu vực mẫu có thể điền đặc biệt

Trong phần này, chúng ta sẽ nói về

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
3,
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
5 và
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
6. Những cái khác được đề cập trong phần "Mẫu được đặt tên" ở phần sau của hướng dẫn này

Nếu khác

Cấu trúc điều khiển đầu tiên mà chúng ta sẽ xem xét là bao gồm các khối văn bản trong một mẫu một cách có điều kiện. Đây là khối

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
3/
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
4

Cấu trúc cơ bản cho một điều kiện trông như thế này

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
2

Lưu ý rằng chúng ta hiện đang nói về đường ống dẫn thay vì giá trị. Lý do cho điều này là để làm rõ rằng các cấu trúc điều khiển có thể thực thi toàn bộ đường ống chứ không chỉ đánh giá một giá trị

Một đường ống được đánh giá là sai nếu giá trị là

  • một boolean sai
  • một số không
  • một chuỗi rỗng
  • một
    # Source: mychart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eyewitness-elk-configmap
    data:
      myvalue: "Hello World"
      drink: "coffee"
      food: "PIZZA"
      mug: "true"
    
    35 (trống hoặc rỗng)
  • một bộ sưu tập rỗng (
    # Source: mychart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eyewitness-elk-configmap
    data:
      myvalue: "Hello World"
      drink: "coffee"
      food: "PIZZA"
      mug: "true"
    
    36,
    # Source: mychart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eyewitness-elk-configmap
    data:
      myvalue: "Hello World"
      drink: "coffee"
      food: "PIZZA"
      mug: "true"
    
    37,
    # Source: mychart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eyewitness-elk-configmap
    data:
      myvalue: "Hello World"
      drink: "coffee"
      food: "PIZZA"
      mug: "true"
    
    38,
    # Source: mychart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eyewitness-elk-configmap
    data:
      myvalue: "Hello World"
      drink: "coffee"
      food: "PIZZA"
      mug: "true"
    
    39,
    # Source: mychart/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eyewitness-elk-configmap
    data:
      myvalue: "Hello World"
      drink: "coffee"
      food: "PIZZA"
      mug: "true"
    
    80)

Trong tất cả các điều kiện khác, điều kiện là đúng

Hãy thêm một điều kiện đơn giản vào ConfigMap của chúng ta. Chúng tôi sẽ thêm một cài đặt khác nếu đồ uống được đặt thành cà phê

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
9

Vì chúng tôi đã nhận xét

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
81 trong ví dụ trước của chúng tôi, đầu ra không được bao gồm cờ
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
82. Nhưng nếu chúng ta thêm dòng đó trở lại vào tệp
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
83 của mình, đầu ra sẽ như thế này

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"

Kiểm soát khoảng trắng

Trong khi xem xét điều kiện, chúng ta nên xem nhanh cách kiểm soát khoảng trắng trong các mẫu. Hãy lấy ví dụ trước và định dạng nó để dễ đọc hơn một chút

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
3

Ban đầu, điều này có vẻ tốt. Nhưng nếu chúng ta chạy nó thông qua template engine, chúng ta sẽ nhận được một kết quả đáng tiếc

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
8

Chuyện gì đã xảy ra thế?

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
6

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
84 được thụt lề không chính xác. Hãy đơn giản là xóa một dòng đó và chạy lại

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
8

Khi chúng tôi gửi nó, chúng tôi sẽ nhận được YAML hợp lệ, nhưng trông vẫn hơi buồn cười

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
9

Lưu ý rằng chúng tôi đã nhận được một vài dòng trống trong YAML của mình. Tại sao?

YAML gán ý nghĩa cho khoảng trắng, vì vậy việc quản lý khoảng trắng trở nên khá quan trọng. May mắn thay, các mẫu Helm có một số công cụ trợ giúp

Đầu tiên, cú pháp dấu ngoặc nhọn của khai báo mẫu có thể được sửa đổi bằng các ký tự đặc biệt để yêu cầu công cụ mẫu cắt khoảng trắng.

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
87 (có thêm dấu gạch ngang và dấu cách) cho biết khoảng trắng nên được cắt sang trái, trong khi
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
88 có nghĩa là khoảng trắng bên phải nên được sử dụng. Hãy cẩn thận. Dòng mới là khoảng trắng

Đảm bảo có khoảng cách giữa

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
89 và phần còn lại của chỉ thị của bạn.
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
60 có nghĩa là "cắt khoảng trắng bên trái và in 3" trong khi
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
61 có nghĩa là "in -3"

Sử dụng cú pháp này, chúng tôi có thể sửa đổi mẫu của mình để loại bỏ những dòng mới đó

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
7

Để làm rõ quan điểm này, hãy điều chỉnh những điều trên và thay thế một

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
62 cho mỗi khoảng trắng sẽ bị xóa theo quy tắc này. Một
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
62 ở cuối dòng cho biết một ký tự dòng mới sẽ bị xóa

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
0

Hãy ghi nhớ điều đó, chúng ta có thể chạy mẫu của mình thông qua Helm và xem kết quả

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
90

Hãy cẩn thận với các công cụ sửa đổi chomping. Thật dễ dàng để vô tình làm những việc như thế này

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
91

Điều đó sẽ tạo ra

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
64 vì nó đã tiêu thụ các dòng mới ở cả hai bên

Để biết chi tiết về kiểm soát khoảng trắng trong các mẫu, hãy xem tài liệu mẫu Go chính thức

Cuối cùng, đôi khi sẽ dễ dàng hơn để cho hệ thống mẫu biết cách thụt lề cho bạn thay vì cố gắng nắm vững khoảng cách của các chỉ thị mẫu. Vì lý do đó, đôi khi bạn có thể thấy hữu ích khi sử dụng hàm

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
65 (
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
66)

Sửa đổi phạm vi sử dụng # Source: mychart/templates/configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: eyewitness-elk-configmap data: myvalue: "Hello World" drink: "coffee" food: "PIZZA" mug: "true" 5

Cấu trúc điều khiển tiếp theo để xem xét là hành động

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
5. Điều này kiểm soát phạm vi biến. Nhớ lại rằng
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
69 là một tham chiếu đến phạm vi hiện tại. Vì vậy,
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
80 yêu cầu mẫu tìm đối tượng
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
81 trong phạm vi hiện tại

Cú pháp của

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
5 tương tự như một câu lệnh
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
3 đơn giản

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
92

Phạm vi có thể được thay đổi.

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
5 có thể cho phép bạn đặt phạm vi hiện tại (
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
69) cho một đối tượng cụ thể. Ví dụ: chúng tôi đã làm việc với
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
86. Hãy viết lại Bản đồ cấu hình của chúng ta để thay đổi phạm vi
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
69 để trỏ đến
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
86

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
93

Lưu ý rằng chúng tôi đã loại bỏ điều kiện

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
3 khỏi bài tập trước vì bây giờ nó không cần thiết - khối sau
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
5 chỉ thực thi nếu giá trị của
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
91 không trống

Lưu ý rằng bây giờ chúng ta có thể tham khảo

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
92 và
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
93 mà không cần định tính chúng. Đó là bởi vì câu lệnh
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
5 đặt
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
69 để trỏ tới
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
86.
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
69 được đặt lại về phạm vi trước đó sau
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
98

Nhưng đây là một lưu ý thận trọng. Bên trong phạm vi hạn chế, bạn sẽ không thể truy cập các đối tượng khác từ phạm vi cha bằng cách sử dụng

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
69. Điều này, ví dụ, sẽ thất bại

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
94

Nó sẽ báo lỗi vì

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
70 không nằm trong phạm vi giới hạn của
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
69. Tuy nhiên, nếu chúng tôi hoán đổi hai dòng cuối cùng, tất cả sẽ hoạt động như mong đợi vì phạm vi được đặt lại sau
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
98

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
95

Hoặc, chúng ta có thể sử dụng

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
73 để truy cập đối tượng
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
70 từ phạm vi cha.
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
73 được ánh xạ tới phạm vi gốc khi quá trình thực thi mẫu bắt đầu và nó không thay đổi trong quá trình thực thi mẫu. Sau đây cũng sẽ hoạt động tốt

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
96

Sau khi xem xét

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
6, chúng tôi sẽ xem xét các biến mẫu, cung cấp một giải pháp cho vấn đề phạm vi ở trên

Vòng lặp với hành động # Source: mychart/templates/configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: eyewitness-elk-configmap data: myvalue: "Hello World" drink: "coffee" food: "PIZZA" mug: "true" 6

Nhiều ngôn ngữ lập trình có hỗ trợ vòng lặp bằng cách sử dụng vòng lặp

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
78, vòng lặp
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
79 hoặc các cơ chế chức năng tương tự. Trong ngôn ngữ mẫu của Helm, cách lặp qua một bộ sưu tập là sử dụng toán tử
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
6

Để bắt đầu, hãy thêm danh sách lớp phủ bánh pizza vào tệp

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
83 của chúng tôi

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
97

Bây giờ chúng tôi có một danh sách (được gọi là

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
37 trong các mẫu) của
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
03. Chúng tôi có thể sửa đổi mẫu của mình để in danh sách này vào Bản đồ cấu hình của chúng tôi

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
98

Chúng ta có thể sử dụng

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
73 để truy cập danh sách
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
05 từ phạm vi cha.
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
73 được ánh xạ tới phạm vi gốc khi quá trình thực thi mẫu bắt đầu và nó không thay đổi trong quá trình thực thi mẫu. Sau đây cũng sẽ hoạt động tốt

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
99

Hãy xem xét kỹ hơn danh sách

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
07. Hàm
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
6 sẽ "phạm vi" (lặp qua) danh sách
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
03. Nhưng bây giờ một cái gì đó thú vị xảy ra. Giống như
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
5 thiết lập phạm vi của
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
69, toán tử
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
6 cũng vậy. Mỗi lần qua vòng lặp,
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
69 được đặt thành lớp phủ bánh pizza hiện tại. Tức là, lần đầu tiên,
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
69 được đặt thành
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
905. Lần lặp lại thứ hai, nó được đặt thành
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
906, v.v.

Chúng tôi có thể gửi giá trị của

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
69 trực tiếp xuống một đường ống dẫn, vì vậy khi chúng tôi thực hiện
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
908, nó sẽ gửi
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
69 tới
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
910 (chức năng kiểu chữ hoa tiêu đề) và sau đó tới
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
911. Nếu chúng tôi chạy mẫu này, đầu ra sẽ là

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
0

Bây giờ, trong ví dụ này, chúng tôi đã thực hiện một số điều phức tạp. Dòng

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
912 đang khai báo một chuỗi nhiều dòng. Vì vậy, danh sách toppings của chúng tôi thực sự không phải là danh sách YAML. Đó là một chuỗi lớn. Tại sao chúng ta sẽ làm điều này? . Để hiểu tại sao lại như vậy, hãy xem tài liệu Kubernetes ConfigMap. Tuy nhiên, đối với chúng tôi, chi tiết này không quan trọng lắm

Điểm đánh dấu

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
914 trong YAML có một chuỗi nhiều dòng. Đây có thể là một kỹ thuật hữu ích để nhúng các khối dữ liệu lớn bên trong bảng kê khai của bạn, như được minh họa ở đây

Đôi khi, thật hữu ích khi có thể nhanh chóng tạo một danh sách bên trong mẫu của bạn, sau đó lặp lại danh sách đó. Các mẫu mũ bảo hiểm có chức năng giúp việc này trở nên dễ dàng.

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
38. Trong khoa học máy tính, một bộ là một tập hợp giống như danh sách có kích thước cố định, nhưng với các kiểu dữ liệu tùy ý. Điều này đại khái truyền đạt cách sử dụng
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
38

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
1

Ở trên sẽ tạo ra cái này

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
2

Ngoài danh sách và bộ dữ liệu,

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
6 có thể được sử dụng để lặp qua các bộ sưu tập có khóa và giá trị (như
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
36 hoặc
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: eyewitness-elk-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: "true"
39). Chúng ta sẽ xem cách thực hiện điều đó trong phần tiếp theo khi chúng ta giới thiệu các biến mẫu

Làm cách nào để sử dụng if lồng nhau trong php?

Câu lệnh if lồng nhau trong PHP .
nếu (điều kiện) {
//code sẽ được thực thi nếu điều kiện đúng
nếu (điều kiện) {
//code sẽ được thực thi nếu điều kiện đúng

Làm cách nào để viết mã php trong tệp TPL?

Nếu bạn muốn thực thi một số mã php trong mẫu tpl thì hãy thực hiện bằng cách gọi hàm php từ mẫu php và không viết mã php trong . Nếu bạn muốn thực thi một số mã php trong mẫu tpl thì hãy thực hiện bằng cách gọi các hàm php từ mẫu php và không viết mã php trong mẫu.

Định dạng tệp TPL là gì?

TPL là tệp mẫu là tệp văn bản phổ biến chứa các biến do người dùng xác định có quyền được ghi đè bởi nội dung đầu ra do người dùng xác định khi ứng dụng web PHP phân tích cú pháp tệp mẫu. Chúng được sử dụng bởi các ứng dụng web với tập lệnh máy chủ PHP (nhưng không bị hạn chế) dưới dạng tệp mẫu

Cú pháp của if other trong php là gì?

Trong PHP chúng ta có các câu lệnh điều kiện sau. câu lệnh if - thực thi một số mã nếu một điều kiện là đúng. nếu. câu lệnh khác - thực thi một số mã nếu một điều kiện là đúng và một mã khác nếu điều kiện đó là sai . nếu. khác. câu lệnh khác - thực thi các mã khác nhau cho hơn hai điều kiện.