1
00:00:02,090 --> 00:00:03,860
So with that up to this point,

2
00:00:03,860 --> 00:00:07,310
we had a very detailed look at resources

3
00:00:07,310 --> 00:00:09,960
being created in a declarative way.

4
00:00:09,960 --> 00:00:12,350
Now I wanna dive back into selectors,

5
00:00:12,350 --> 00:00:14,780
because it's such a crucial feature.

6
00:00:14,780 --> 00:00:16,820
I already explained that you use it

7
00:00:16,820 --> 00:00:20,100
to connect other resources to a resource,

8
00:00:20,100 --> 00:00:23,080
for example, to connect paths to this deployment

9
00:00:23,080 --> 00:00:26,143
or connect paths to this service.

10
00:00:26,990 --> 00:00:28,040
You also learned that

11
00:00:28,040 --> 00:00:29,990
there are different kinds of selectors,

12
00:00:29,990 --> 00:00:32,090
this is very basic label selector,

13
00:00:32,090 --> 00:00:33,650
which you have on some objects

14
00:00:33,650 --> 00:00:37,000
like this service and this more modern selector

15
00:00:37,000 --> 00:00:40,660
where you have to define that you wanna match labels.

16
00:00:40,660 --> 00:00:43,600
So what you have in match labels is then the same

17
00:00:43,600 --> 00:00:46,950
as you have here in this service selector.

18
00:00:46,950 --> 00:00:47,783
Now, when you have an object

19
00:00:47,783 --> 00:00:52,520
that supports this more modern syntax like deployment,

20
00:00:52,520 --> 00:00:55,170
you have an alternative to match labels.

21
00:00:55,170 --> 00:00:59,510
You also have match expressions.

22
00:00:59,510 --> 00:01:02,560
And you find this in your official reference documentation,

23
00:01:02,560 --> 00:01:07,380
of course, there under deployment spec.

24
00:01:07,380 --> 00:01:09,600
If you have a look at the selector,

25
00:01:09,600 --> 00:01:11,950
you see that there you can match labels,

26
00:01:11,950 --> 00:01:15,043
which is what we did thus far or match expressions.

27
00:01:16,800 --> 00:01:21,070
And match expressions is a more powerful way

28
00:01:21,070 --> 00:01:23,030
of selecting things

29
00:01:23,030 --> 00:01:26,890
where you simply have more configuration options

30
00:01:26,890 --> 00:01:28,173
where you can do more.

31
00:01:29,330 --> 00:01:31,260
And here's how it works.

32
00:01:31,260 --> 00:01:34,410
Unlike match labels, you don't have multiple nest

33
00:01:34,410 --> 00:01:36,810
of key value pairs off labels,

34
00:01:36,810 --> 00:01:39,360
which are added to some other template,

35
00:01:39,360 --> 00:01:41,750
but instead you have a list of expressions,

36
00:01:41,750 --> 00:01:43,840
which all have to be satisfied

37
00:01:43,840 --> 00:01:46,590
in order to have a matching object.

38
00:01:46,590 --> 00:01:50,200
And an expression is then defined in curly braces,

39
00:01:50,200 --> 00:01:52,463
and you identify a key,

40
00:01:53,510 --> 00:01:56,580
something like app, for example,

41
00:01:56,580 --> 00:01:58,590
referring to a label key.

42
00:01:58,590 --> 00:02:00,070
So that is always the case.

43
00:02:00,070 --> 00:02:02,220
We're still selecting on labels here.

44
00:02:02,220 --> 00:02:05,620
We now can just express more different things

45
00:02:05,620 --> 00:02:08,850
on how to select, but we still refer to labels.

46
00:02:08,850 --> 00:02:12,840
So here, you first start by defining a label key.

47
00:02:12,840 --> 00:02:17,390
Then you define the values which you wanna allow.

48
00:02:17,390 --> 00:02:20,480
And that now is between square brackets.

49
00:02:20,480 --> 00:02:23,300
And here you now have a list of values

50
00:02:23,300 --> 00:02:26,600
you wanna accept like second app and first app,

51
00:02:26,600 --> 00:02:29,223
for example, separated by commas.

52
00:02:30,940 --> 00:02:32,900
But the interesting thing now is that

53
00:02:32,900 --> 00:02:35,380
you can also add an operator.

54
00:02:35,380 --> 00:02:38,710
And here, only a couple of operators are allowed.

55
00:02:38,710 --> 00:02:41,410
To be precise, in the official reference docs,

56
00:02:41,410 --> 00:02:44,210
you'll see that you can have in, not in,

57
00:02:44,210 --> 00:02:47,040
exists and does not exist.

58
00:02:47,040 --> 00:02:49,790
So, for example, we could set the operator to in here.

59
00:02:49,790 --> 00:02:53,020
And that would mean we wanna select all paths

60
00:02:53,020 --> 00:02:57,130
where the app label has a value in this range,

61
00:02:57,130 --> 00:03:00,440
so one of these values, which of course would be true

62
00:03:00,440 --> 00:03:03,470
for this path, because it has an app label

63
00:03:03,470 --> 00:03:05,250
with a value of second app,

64
00:03:05,250 --> 00:03:09,970
which is one of the two supported labels specified here.

65
00:03:09,970 --> 00:03:12,130
And of course, just to clarify this,

66
00:03:12,130 --> 00:03:15,180
we don't need this more complex expression here.

67
00:03:15,180 --> 00:03:17,840
This way of selecting. It was totally fine.

68
00:03:17,840 --> 00:03:21,390
And it is the approach you will most commonly use.

69
00:03:21,390 --> 00:03:25,110
But if you need more power, if you need more flexibility,

70
00:03:25,110 --> 00:03:27,710
you could use a match expression like this.

71
00:03:27,710 --> 00:03:30,470
And there, you can also exclude things.

72
00:03:30,470 --> 00:03:32,617
You could say, "I wanna select paths,

73
00:03:32,617 --> 00:03:36,440
"which are not in this range of values."

74
00:03:36,440 --> 00:03:38,830
Now this path would not be selected.

75
00:03:38,830 --> 00:03:41,160
And therefore, of course, this here technically

76
00:03:41,160 --> 00:03:45,120
would be an incorrect selector for this deployment,

77
00:03:45,120 --> 00:03:47,790
because we wouldn't connect it to the paths,

78
00:03:47,790 --> 00:03:50,630
which it creates, which is not allowed.

79
00:03:50,630 --> 00:03:55,160
A deployment always needs to select the paths it creates.

80
00:03:55,160 --> 00:03:56,890
I just want to mention and show you

81
00:03:56,890 --> 00:03:59,200
this match expression's syntax.

82
00:03:59,200 --> 00:04:00,730
We don't need it here, though.

83
00:04:00,730 --> 00:04:02,970
We will go back to match labels,

84
00:04:02,970 --> 00:04:05,010
but it is worth knowing this as well

85
00:04:05,010 --> 00:04:07,973
and understanding what the selectors are about.

86
00:04:08,910 --> 00:04:12,390
Now, as a side note, you cannot just use selectors here

87
00:04:12,390 --> 00:04:15,690
when you are working in this declarative approach.

88
00:04:15,690 --> 00:04:18,980
Also when running commands, you can use selectors,

89
00:04:18,980 --> 00:04:21,440
for example, with the delete command,

90
00:04:21,440 --> 00:04:23,800
We learned that you can delete something

91
00:04:23,800 --> 00:04:27,110
by pointing at the file where it was defined,

92
00:04:27,110 --> 00:04:31,060
or by targeting a deployment or a service by name.

93
00:04:31,060 --> 00:04:32,880
That's what we saw earlier.

94
00:04:32,880 --> 00:04:35,460
Well, you can also delete by selector.

95
00:04:35,460 --> 00:04:39,510
By adding the -l flag for label,

96
00:04:39,510 --> 00:04:43,850
you can select objects by label, which should be deleted.

97
00:04:43,850 --> 00:04:46,500
And to show this to you, let's make sure

98
00:04:46,500 --> 00:04:50,110
that we got up and running deployments, which I do.

99
00:04:50,110 --> 00:04:53,670
And let's now delete this deployment by label.

100
00:04:53,670 --> 00:04:56,230
For this in the metadata of this deployment,

101
00:04:56,230 --> 00:04:58,660
I will, first of all, add labels,

102
00:04:58,660 --> 00:05:01,170
because otherwise we can select it by label.

103
00:05:01,170 --> 00:05:05,250
So here, I'll give this a label of group.

104
00:05:05,250 --> 00:05:08,240
Again, this is up to you, this key name,

105
00:05:08,240 --> 00:05:11,120
which I'll name example,

106
00:05:11,120 --> 00:05:14,980
and I'll add this same label to my service here, by the way.

107
00:05:14,980 --> 00:05:17,380
So in the metadata of this service,

108
00:05:17,380 --> 00:05:21,330
I will add labels, and I will add this group example label

109
00:05:21,330 --> 00:05:23,230
to my service.

110
00:05:23,230 --> 00:05:25,750
So it's now added to the metadata of the service

111
00:05:25,750 --> 00:05:27,570
and to the deployment.

112
00:05:27,570 --> 00:05:30,130
And with that done, we now, first of all,

113
00:05:30,130 --> 00:05:34,610
apply that, and I changed it in these separate files.

114
00:05:34,610 --> 00:05:36,850
So they are forced since I before deployed it

115
00:05:36,850 --> 00:05:38,470
with the combined file.

116
00:05:38,470 --> 00:05:40,080
I will, first of all, delete

117
00:05:40,940 --> 00:05:43,740
the master deployment

118
00:05:45,517 --> 00:05:47,770
.yaml file, not the file,

119
00:05:47,770 --> 00:05:50,790
but the things that were created based on it.

120
00:05:50,790 --> 00:05:55,790
And then, I will apply the deployment.yaml file

121
00:05:55,910 --> 00:06:00,830
and also the service.yaml file,

122
00:06:00,830 --> 00:06:02,660
so that this has created.

123
00:06:02,660 --> 00:06:05,950
And now we, of course, got these deployments,

124
00:06:05,950 --> 00:06:10,950
and we got this service up and running, as we can see.

125
00:06:12,500 --> 00:06:16,970
And with that, we can now delete by label with -l,

126
00:06:16,970 --> 00:06:20,430
and then after dead adding a key value pair

127
00:06:20,430 --> 00:06:23,300
of group=example.

128
00:06:23,300 --> 00:06:25,920
That's how you add this -l option.

129
00:06:25,920 --> 00:06:27,960
So there's no equal sign after the option,

130
00:06:27,960 --> 00:06:32,310
but instead they are after you have your key value pairs.

131
00:06:32,310 --> 00:06:34,543
In this case, group=example.

132
00:06:35,430 --> 00:06:37,440
Now, the only thing you gotta do is

133
00:06:37,440 --> 00:06:40,500
to not accidentally delete more than you want

134
00:06:40,500 --> 00:06:43,180
as an extra security mechanism, so to say,

135
00:06:43,180 --> 00:06:45,840
you have to let the delete command know

136
00:06:45,840 --> 00:06:48,670
which kind of resources you wanna delete.

137
00:06:48,670 --> 00:06:51,823
So here I'll say deployments,services.

138
00:06:53,040 --> 00:06:56,010
And this tells to command that we wanna delete deployments

139
00:06:56,010 --> 00:06:58,730
and services, which have these labels.

140
00:06:58,730 --> 00:07:01,530
And if we now hit enter, indeed, you see

141
00:07:01,530 --> 00:07:04,090
it's deleted what it should delete.

142
00:07:04,090 --> 00:07:06,200
We got no deployments left,

143
00:07:06,200 --> 00:07:08,870
and, of course, also no services,

144
00:07:08,870 --> 00:07:11,520
except for the default service, which we always have.

145
00:07:12,520 --> 00:07:15,510
So that's a closer look at labels and selectors.

146
00:07:15,510 --> 00:07:18,310
And I hope with that, it's clear why we have that.

147
00:07:18,310 --> 00:07:21,590
It is important to understand that with selectors,

148
00:07:21,590 --> 00:07:24,950
you can only select by labels, defined in metadata,

149
00:07:24,950 --> 00:07:27,050
not by name, for example.

150
00:07:27,050 --> 00:07:28,790
The name can be used.

151
00:07:28,790 --> 00:07:30,800
For example, with the delete command,

152
00:07:30,800 --> 00:07:33,730
we can delete a deployment by name.

153
00:07:33,730 --> 00:07:36,140
That's what we saw earlier in the course already.

154
00:07:36,140 --> 00:07:39,080
But if you want to use the -l flag here,

155
00:07:39,080 --> 00:07:42,700
or if you use a selector in the declarative definition

156
00:07:42,700 --> 00:07:46,660
of some other resource, we're always talking about labels

157
00:07:46,660 --> 00:07:47,633
in the metadata.

