So with that up to this point,
we had a very detailed look at resources
being created in a declarative way.
Now I wanna dive back into selectors,
because it's such a crucial feature.
I already explained that you use it
to connect other resources to a resource,
for example, to connect paths to this deployment
or connect paths to this service.
You also learned that
there are different kinds of selectors,
this is very basic label selector,
which you have on some objects
like this service and this more modern selector
where you have to define that you wanna match labels.
So what you have in match labels is then the same
as you have here in this service selector.
Now, when you have an object
that supports this more modern syntax like deployment,
you have an alternative to match labels.
You also have match expressions.
And you find this in your official reference documentation,
of course, there under deployment spec.
If you have a look at the selector,
you see that there you can match labels,
which is what we did thus far or match expressions.
And match expressions is a more powerful way
of selecting things
where you simply have more configuration options
where you can do more.
And here's how it works.
Unlike match labels, you don't have multiple nest
of key value pairs off labels,
which are added to some other template,
but instead you have a list of expressions,
which all have to be satisfied
in order to have a matching object.
And an expression is then defined in curly braces,
and you identify a key,
something like app, for example,
referring to a label key.
So that is always the case.
We're still selecting on labels here.
We now can just express more different things
on how to select, but we still refer to labels.
So here, you first start by defining a label key.
Then you define the values which you wanna allow.
And that now is between square brackets.
And here you now have a list of values
you wanna accept like second app and first app,
for example, separated by commas.
But the interesting thing now is that
you can also add an operator.
And here, only a couple of operators are allowed.
To be precise, in the official reference docs,
you'll see that you can have in, not in,
exists and does not exist.
So, for example, we could set the operator to in here.
And that would mean we wanna select all paths
where the app label has a value in this range,
so one of these values, which of course would be true
for this path, because it has an app label
with a value of second app,
which is one of the two supported labels specified here.
And of course, just to clarify this,
we don't need this more complex expression here.
This way of selecting. It was totally fine.
And it is the approach you will most commonly use.
But if you need more power, if you need more flexibility,
you could use a match expression like this.
And there, you can also exclude things.
You could say, "I wanna select paths,
"which are not in this range of values."
Now this path would not be selected.
And therefore, of course, this here technically
would be an incorrect selector for this deployment,
because we wouldn't connect it to the paths,
which it creates, which is not allowed.
A deployment always needs to select the paths it creates.
I just want to mention and show you
this match expression's syntax.
We don't need it here, though.
We will go back to match labels,
but it is worth knowing this as well
and understanding what the selectors are about.
Now, as a side note, you cannot just use selectors here
when you are working in this declarative approach.
Also when running commands, you can use selectors,
for example, with the delete command,
We learned that you can delete something
by pointing at the file where it was defined,
or by targeting a deployment or a service by name.
That's what we saw earlier.
Well, you can also delete by selector.
By adding the -l flag for label,
you can select objects by label, which should be deleted.
And to show this to you, let's make sure
that we got up and running deployments, which I do.
And let's now delete this deployment by label.
For this in the metadata of this deployment,
I will, first of all, add labels,
because otherwise we can select it by label.
So here, I'll give this a label of group.
Again, this is up to you, this key name,
which I'll name example,
and I'll add this same label to my service here, by the way.
So in the metadata of this service,
I will add labels, and I will add this group example label
to my service.
So it's now added to the metadata of the service
and to the deployment.
And with that done, we now, first of all,
apply that, and I changed it in these separate files.
So they are forced since I before deployed it
with the combined file.
I will, first of all, delete
the master deployment
.yaml file, not the file,
but the things that were created based on it.
And then, I will apply the deployment.yaml file
and also the service.yaml file,
so that this has created.
And now we, of course, got these deployments,
and we got this service up and running, as we can see.
And with that, we can now delete by label with -l,
and then after dead adding a key value pair
of group=example.
That's how you add this -l option.
So there's no equal sign after the option,
but instead they are after you have your key value pairs.
In this case, group=example.
Now, the only thing you gotta do is
to not accidentally delete more than you want
as an extra security mechanism, so to say,
you have to let the delete command know
which kind of resources you wanna delete.
So here I'll say deployments,services.
And this tells to command that we wanna delete deployments
and services, which have these labels.
And if we now hit enter, indeed, you see
it's deleted what it should delete.
We got no deployments left,
and, of course, also no services,
except for the default service, which we always have.
So that's a closer look at labels and selectors.
And I hope with that, it's clear why we have that.
It is important to understand that with selectors,
you can only select by labels, defined in metadata,
not by name, for example.
The name can be used.
For example, with the delete command,
we can delete a deployment by name.
That's what we saw earlier in the course already.
But if you want to use the -l flag here,
or if you use a selector in the declarative definition
of some other resource, we're always talking about labels
in the metadata.